| | |
C++ Performance Tips
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
> 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
Simple, obvious, and more importantly, TRANSPARENT to the optimiser. My recent version of gcc for example optimised this out to (wait for it)
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.
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)
{ int temp = a ; a = b ; b = temp; } printf( "%d %d\n", a, b );
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.
Optimizing Software in C++
http://www.agner.org/optimize/optimizing_cpp.pdf
Agner has a few other manuals, on his site too.
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
An easy way to swap 2 variables without using another variable:
C++ Syntax (Toggle Plain Text)
a=a+b; b=a-b; a=a-b;
C++ Syntax (Toggle Plain Text)
a = a + b - (b = a);
•
•
Join Date: Mar 2008
Posts: 1,399
Reputation:
Solved Threads: 113
•
•
•
•
>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)
#include <climits> #include <ctime> #include <iostream> void swap_temp ( int& a, int& b ) { int temp = a; a = b; b = temp; } void swap_xor ( int& a, int& b ) { a ^= b; b ^= a; a ^= b; } int main() { int a1 = 10, a2 = 20; int b1 = 10, b2 = 20; std::clock_t start; std::cout<< a1 <<"\t"<< a2 <<'\n'; std::cout<< b1 <<"\t"<< b2 <<'\n'; swap_temp ( a1, a2 ); swap_temp ( b1, b2 ); std::cout<< a1 <<"\t"<< a2 <<'\n'; std::cout<< b1 <<"\t"<< b2 <<'\n'; std::cout<<"swap_temp: "; start = std::clock(); for ( unsigned i = 0; i < UINT_MAX; i++ ) swap_temp ( a1, a2 ); std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n'; std::cout<<"swap_xor: "; start = std::clock(); for ( unsigned i = 0; i < UINT_MAX; i++ ) swap_temp ( b1, b2 ); std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n'; }
![]() |
Similar Threads
- improve performance of the following io codes (C)
- Performance Improvements (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Help with an overloaded operator
- Next Thread: Combined accessor/mutator?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






