Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-13-2003, 07:07 PM   #1 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Lol

For C++ programmers:

According to Microsoft the prefix iterator ++ is faster then the postfix version.
So ++i is faster then i++.

I've tested this. Let me present the code first.

Code:
 // PerformanceCheck.cpp : Defines the entry point for the console application.
//

#include "stdafx.h" //Holds <iostream.h>, <time.h>

long double i = 0;

int main(int argc, char* argv[])
{
	clock_t start, end;
	long double duration;
	start=clock();
	for(i;i<4000000000;i++)
	{i++;}
	end=clock();
	duration=(long double)(end-start)/CLOCKS_PER_SEC;
	cout<<"Elapsed time: "<<duration<<endl;

	return 0;	
}
Check out "i". This loop (four billion!) executes "i++" (and ++i) only.

In the code, I take the time before the loop starts and after the loop ends.
Result on my 2GHz machine:
i++: between 11.093 seconds and 11.125 seconds.
++i: EXACTLY the same.
Test repeats: 1000.
*.exe mode: release, optimization for speed. One thread.

Congratulation Microsoft in fooling tons of programmers.

Why did I test this?
I am currently involved in a beta testers group for BattleZone2 1.3b. The lead programmer added a comment that he changed all instances of "i++" to "++i" because MS said it is slightly faster. Luckily the programmer said he doesn't trust MS but he managed to change all the instances without testing!!

ROFL. As soon as the BZ2 boards are up again ima gonna post this hehe
Valmont is offline   Reply With Quote
Old 07-13-2003, 08:49 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
hah!
__________________
Mike
sde is offline   Reply With Quote
Old 07-13-2003, 08:59 PM   #3 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
lol..

and i thought it was faster.. but i haven't tested it. i guess if it is, it isn't noticably so..
alpha is offline   Reply With Quote
Old 07-13-2003, 11:44 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
In theory it should be slightly faster, given that the postfix operator will make the earlier value of your itterator available for use in the same line. Where as the prefix operator changes the value instantly.
If you made some serius compiler optimization on that, you might end up with saving one or two instructions in the assembly code.. But then again, that'll be one clock-cycle in a 2Ghz CPU.. Not that much anyway.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-14-2003, 01:53 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
It might be that a prefix increment adds a value first, but this is useless in a loop. I the end the fully executed loop takes 5 clock cycles to execute.

Lets find out more about this. Maybe it is relevant in nano-second timing programs.
Valmont is offline   Reply With Quote
Old 07-14-2003, 01:39 PM   #6 (permalink)
Unicorn
Registered User
 
Unicorn's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 11
Unicorn is on a distinguished road
I tried a few situations with gcc 3.2, it seems that for builtin types the prefix and postfix increment operator result in exactly the same assembly code.

For other iterators (e.g. STL), it depends on the kind of iterator and the optimization flags you use. In some cases there is no difference, in some cases prefix is faster.

I don't see any reason to use postfix.
Unicorn is offline   Reply With Quote
Old 08-03-2003, 11:37 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
I've promised to find out more about this, so I did:

There is a difference in the interface (code) between post and pre increment operators in certain data types.
The post increment does create a temporary variable and the temp var might call the constructor of it's class for it's instance. This slows down the execution time.
So to prevent this (creating the temp variable) one should use the pre increment to make sure no un-needed constructor calls are made.

Remember that constructor calls are slow and destructor calls are even slower. Those are the basics, but always handy to to write down again .

Phew, another lesson learned .
__________________
Valmont is offline   Reply With Quote
Old 04-08-2005, 01:27 AM   #8 (permalink)
killemov
Registered User
 
Join Date: Apr 2005
Posts: 1
killemov is on a distinguished road
I just stumbled onto this thread ... (bit late)
Code:
	for(i;i<4000000000;i++)
	{i++;}
the problem is of course in incrementing i inside the loop. If you do not touch the loop iterator, using the prefix incrementor will be optimized better.
killemov is offline   Reply With Quote
Old 04-08-2005, 02:31 AM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
It still doesn't matter. No temporaries are made so this doesn't have any effect.
It will matter where a post-increment creates a temporary.
Quote:
...using the prefix incrementor will be optimized better.
Assembly code wise not at all.
__________________
Valmont is offline   Reply With Quote
Old 04-08-2005, 09:45 AM   #10 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Isn't blithely changing all post increments to preincrements a recipe for subtle bugs, if the original programmer was counting on seeing the old value then incrementing?
teknomage1 is offline   Reply With Quote
Old 04-08-2005, 11:46 AM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Well in that case you shouldn't change it.
But in loops usually you should use the pre-increment. Although with built in types it doesn't matter. That I did test.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
lol. Admin Lounge 2 09-20-2004 09:49 AM
LOL...check this out M3GAPL3X Lounge 0 08-04-2003 03:06 PM
Lol Ilya020 Lounge 4 05-02-2003 08:58 PM
lol :) Ilya020 Lounge 0 01-18-2003 06:26 PM
Longhorn w00t Linux / BSD / OS X 2 12-23-2002 06:23 PM


All times are GMT -8. The time now is 05:08 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting