C++ Performance Tips

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C++ Performance Tips

 
3
  #41
Jun 20th, 2008
> 1. In VS 6.0 (on Intel H/W) (are unsigned better?)
So, you pick one specific processor / compiler combination out of the many 10's of processors (or maybe hundreds of variants, or thousands of compilers + flag variations) and decide that proves it?
Interesting it may be, but it's not in my "useful" things to know when it comes to getting the best out of the code.

The whole point of using a HLL is to stop you from having to worry about the minutia of how specific machines handle specific cases, and thus allow you to concentrate on the bigger issues.

Any (let me say it again for the hard of reading, ANY) attempt at performance optimisation before you've finished the program and done some meaningful profiling is a waste of time. Focus your effort on choosing good data structures and algorithms, which will save hours (or months) of computing time, and let the compiler worry about the microseconds.

Your fast loop won't mean squat if you were dumb enough to put it in a bubble sort for example. There isn't an optimiser out there which can spot a bubble sort and decide to replace it with quicksort. That's YOUR job, so do it.

If you're finding your "optimised" code hard to read, then for sure so will the compilers' optimiser, and it will just chicken out and do exactly what you asked for.

Take those ridiculous attempts at swapping variables (without a temp) for example.

What could possibly be wrong with
  1. { int temp = a ; a = b ; b = temp; }
  2. printf( "%d %d\n", a, b );
Simple, obvious, and more importantly, TRANSPARENT to the optimiser. My recent version of gcc for example optimised this out to (wait for it)
printf( "%d %d\n", b, a ); That's right, the swap is GONE!

An extreme case maybe, but it does illustrate that if the code is clear in it's purpose, then the optimiser may just get rid of it and find another way to get to where you want to be.

With all those swap tricks, the optimiser is left with "Huh?, WTF is that" and you get exactly what you asked for.

Compilers have evolved a lot since the 1980's, so stop recycling all those 1980's tricks. They either don't work, are unnecessary or just make it worse.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 342
Reputation: x3mario is an unknown quantity at this point 
Solved Threads: 1
x3mario's Avatar
x3mario x3mario is offline Offline
Posting Whiz

Re: C++ Performance Tips

 
0
  #42
Sep 17th, 2008
I have picked some ideas here about c++. Thanks gurus.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: C++ Performance Tips

 
1
  #43
Nov 13th, 2008
Optimizing Software in C++
http://www.agner.org/optimize/optimizing_cpp.pdf

Agner has a few other manuals, on his site too.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: presario is an unknown quantity at this point 
Solved Threads: 0
presario presario is offline Offline
Newbie Poster

Re: C++ Performance Tips

 
-2
  #44
Dec 29th, 2008
Originally Posted by bitforce View Post
An easy way to swap 2 variables without using another variable:

  1. a=a+b;
  2. b=a-b;
  3. a=a-b;
here's a one-statement version:
  1. a = a + b - (b = a);
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C++ Performance Tips

 
0
  #45
Dec 29th, 2008
> here's a one-statement version:
Which is just as broken as all the other 1-statement attempts, which result in a variable (in this case b) being assigned before (or maybe after, who knows) the rest of the expression.
Read the thread, learn about sequence points.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: C++ Performance Tips

 
0
  #46
Jan 18th, 2009
Originally Posted by Narue View Post
>The most efficient way for swapping whole numbers is <snip xor swap>
>because xor is an assembly level instruction and its much faster than any move operation.
An empirical test disproves your claim. The swap using a temporary is noticeably faster on at least one compiler (Microsoft C++) given any optimization level.
  1. #include <climits>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. void swap_temp ( int& a, int& b )
  6. {
  7. int temp = a;
  8. a = b;
  9. b = temp;
  10. }
  11.  
  12. void swap_xor ( int& a, int& b )
  13. {
  14. a ^= b;
  15. b ^= a;
  16. a ^= b;
  17. }
  18.  
  19. int main()
  20. {
  21. int a1 = 10, a2 = 20;
  22. int b1 = 10, b2 = 20;
  23. std::clock_t start;
  24.  
  25. std::cout<< a1 <<"\t"<< a2 <<'\n';
  26. std::cout<< b1 <<"\t"<< b2 <<'\n';
  27.  
  28. swap_temp ( a1, a2 );
  29. swap_temp ( b1, b2 );
  30.  
  31. std::cout<< a1 <<"\t"<< a2 <<'\n';
  32. std::cout<< b1 <<"\t"<< b2 <<'\n';
  33.  
  34. std::cout<<"swap_temp: ";
  35. start = std::clock();
  36. for ( unsigned i = 0; i < UINT_MAX; i++ )
  37. swap_temp ( a1, a2 );
  38. std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n';
  39.  
  40. std::cout<<"swap_xor: ";
  41. start = std::clock();
  42. for ( unsigned i = 0; i < UINT_MAX; i++ )
  43. swap_temp ( b1, b2 );
  44. std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n';
  45. }
I think I just noticed a mistake You're comparing swap_temp for both tests, not quite sure why one takes longer than the other though?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,579
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Performance Tips

 
0
  #47
Jan 18th, 2009
>I think I just noticed a mistake
Transcription error. The stated result comes from correct code though.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: Lisa1110 is an unknown quantity at this point 
Solved Threads: 0
Lisa1110 Lisa1110 is offline Offline
Newbie Poster

Re: C++ Performance Tips

 
-1
  #48
Jan 22nd, 2009
Before reading your article I don't have any idea about this . Thank's for giving me knowledge about a new thing.
Last edited by Narue; Jan 22nd, 2009 at 9:23 am. Reason: snipped spam link
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC