944,175 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 78325
  • C++ RSS
You are currently viewing page 5 of this multi-page discussion thread; Jump to the first page
Jun 20th, 2008
3

Re: C++ Performance Tips

> 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
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 17th, 2008
0

Re: C++ Performance Tips

I have picked some ideas here about c++. Thanks gurus.
Reputation Points: 44
Solved Threads: 1
Posting Whiz
x3mario is offline Offline
341 posts
since Aug 2007
Nov 13th, 2008
1

Re: C++ Performance Tips

Optimizing Software in C++
http://www.agner.org/optimize/optimizing_cpp.pdf

Agner has a few other manuals, on his site too.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 29th, 2008
-3

Re: C++ Performance Tips

Click to Expand / Collapse  Quote originally posted by bitforce ...
An easy way to swap 2 variables without using another variable:

C++ Syntax (Toggle Plain Text)
  1. a=a+b;
  2. b=a-b;
  3. a=a-b;
here's a one-statement version:
C++ Syntax (Toggle Plain Text)
  1. a = a + b - (b = a);
Reputation Points: 9
Solved Threads: 0
Newbie Poster
presario is offline Offline
3 posts
since Dec 2008
Dec 29th, 2008
0

Re: C++ Performance Tips

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 18th, 2009
0

Re: C++ Performance Tips

Click to Expand / Collapse  Quote originally posted by Narue ...
>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.
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jan 18th, 2009
1

Re: C++ Performance Tips

>I think I just noticed a mistake
Transcription error. The stated result comes from correct code though.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 22nd, 2009
-1

Re: C++ Performance Tips

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
Reputation Points: 8
Solved Threads: 0
Newbie Poster
Lisa1110 is offline Offline
2 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with an overloaded operator
Next Thread in C++ Forum Timeline: Combined accessor/mutator?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC