Speed again
-
Hi there. Lets say that I want to see wich way of output is faster: "printf" or "cout". I would writte something like this: #include #include int main( void ) { time_t begin, end; begin = time( NULL ); unsigned long i; for ( i=0 ; i<4294967295 ; i++ ) printf( "%d\n", i ); // This is for printf. If I was testing cout it would be // cout << i << endl; end = time( NULL ); printf( "IT TOOK: %d\n", end - begin ); return 0; } My intention is not to test wich of those is faster but to have an algorithm to test the speed for any application. The problem with this implementation is that it is not very precise; computers work a lot faster than what can be measured with seconds. Any suggestions? Regards. hint_54
hint_54 wrote:
wich way of output is faster: "printf" or "cout".
fprintf()
certainly. but the 2 ones are no way comparable.cout
is 100% OO... you could however test usingGetTickCounter()
... this is the best way to know.
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
hint_54 wrote:
wich way of output is faster: "printf" or "cout".
fprintf()
certainly. but the 2 ones are no way comparable.cout
is 100% OO... you could however test usingGetTickCounter()
... this is the best way to know.
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
hint_54 wrote:
wich way of output is faster: "printf" or "cout".
fprintf()
certainly. but the 2 ones are no way comparable.cout
is 100% OO... you could however test usingGetTickCounter()
... this is the best way to know.
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...]Actually I'm not getting the results I was especting with this code. int main( void ) { unsigned long Begin, End, i; tVar *tVariant; // Marks app init Begin = GetTickCount(); // Writte testing stuff here for ( i=0 ; i<500 ; i++ ) { // 4294967295 tVariant = new tVar; tVariant->TContextData = i; delete tVariant; } // Marks app end End = GetTickCount(); printf( "\nIT TOOK: %ld\n", End - Begin ); return 0; } I get an output of: "IT TOOL: 0". Know why!? :confused: regards hint_54
-
Actually I'm not getting the results I was especting with this code. int main( void ) { unsigned long Begin, End, i; tVar *tVariant; // Marks app init Begin = GetTickCount(); // Writte testing stuff here for ( i=0 ; i<500 ; i++ ) { // 4294967295 tVariant = new tVar; tVariant->TContextData = i; delete tVariant; } // Marks app end End = GetTickCount(); printf( "\nIT TOOK: %ld\n", End - Begin ); return 0; } I get an output of: "IT TOOL: 0". Know why!? :confused: regards hint_54
hint_54 wrote:
I get an output of: "IT TOOL: 0". Know why!?
Because the loop is only executing 500 times.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
hint_54 wrote:
I get an output of: "IT TOOL: 0". Know why!?
Because the loop is only executing 500 times.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
Not when what you are wanting to measure happens in just a few ms. You can increase the number of times the loop executes (e.g., 250,000) to verify that it is being timed. To get a little more granularity, you can use the multimedia timers. Also check out
QueryPerformanceCounter()
and its API. Because Windows is not a RTOS, you'll do no better than about 10ms for a Windows NT-based machine.
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
-
Actually I'm not getting the results I was especting with this code. int main( void ) { unsigned long Begin, End, i; tVar *tVariant; // Marks app init Begin = GetTickCount(); // Writte testing stuff here for ( i=0 ; i<500 ; i++ ) { // 4294967295 tVariant = new tVar; tVariant->TContextData = i; delete tVariant; } // Marks app end End = GetTickCount(); printf( "\nIT TOOK: %ld\n", End - Begin ); return 0; } I get an output of: "IT TOOL: 0". Know why!? :confused: regards hint_54
hint_54 wrote:
I get an output of: "IT TOOL: 0". Know why!?
If you are building it in Release mode, I bet the compiler simply optimized away your loop.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
hint_54 wrote:
I get an output of: "IT TOOL: 0". Know why!?
If you are building it in Release mode, I bet the compiler simply optimized away your loop.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Hi there. Lets say that I want to see wich way of output is faster: "printf" or "cout". I would writte something like this: #include #include int main( void ) { time_t begin, end; begin = time( NULL ); unsigned long i; for ( i=0 ; i<4294967295 ; i++ ) printf( "%d\n", i ); // This is for printf. If I was testing cout it would be // cout << i << endl; end = time( NULL ); printf( "IT TOOK: %d\n", end - begin ); return 0; } My intention is not to test wich of those is faster but to have an algorithm to test the speed for any application. The problem with this implementation is that it is not very precise; computers work a lot faster than what can be measured with seconds. Any suggestions? Regards. hint_54
Use the QueryPerformanceCounter API's - look it up on MSDN/Google, and you'll should see tons of examples of how it's used. That's alot more accurate (I think) than GetTickCount(). ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF!
-
Hi there. Lets say that I want to see wich way of output is faster: "printf" or "cout". I would writte something like this: #include #include int main( void ) { time_t begin, end; begin = time( NULL ); unsigned long i; for ( i=0 ; i<4294967295 ; i++ ) printf( "%d\n", i ); // This is for printf. If I was testing cout it would be // cout << i << endl; end = time( NULL ); printf( "IT TOOK: %d\n", end - begin ); return 0; } My intention is not to test wich of those is faster but to have an algorithm to test the speed for any application. The problem with this implementation is that it is not very precise; computers work a lot faster than what can be measured with seconds. Any suggestions? Regards. hint_54
Hello hint_54 When I would like to know how much time it takes to execute a particulary piece of code and this piece of code is very short I read the CPU time stamp twice. (Assuming that you are running a pentium class processor) The CPU time stamp is a 64 bits counter that runs on the same frequency as your CPU does. So with this you ll be able to measure very short times very accurate. Oke this function is written in asm for the Borland compiler. //--------------------------------------------------------------------------- //Read CPU time stamp //--------------------------------------------------------------------------- __int64 CWaveIn::CPUTimeStamp(void) { asm { xor eax,eax xor ebx,ebx xor ecx,ecx xor edx,edx cpuid rdtsc } } //--------------------------------------------------------------------------- The instruction rdtsc reads the CPU time stamp and returns its value in register eax and edx. So you could do the folowing ULONGLONG qwStartTime; ULONGLONG qwStopTime; qwStartTime=CPUTimeStamp(); //Place code here... qwStopTime=CPUTimeStamp(); //Time? qwStopTime-qwStartTime; Succes :)
-
Hello hint_54 When I would like to know how much time it takes to execute a particulary piece of code and this piece of code is very short I read the CPU time stamp twice. (Assuming that you are running a pentium class processor) The CPU time stamp is a 64 bits counter that runs on the same frequency as your CPU does. So with this you ll be able to measure very short times very accurate. Oke this function is written in asm for the Borland compiler. //--------------------------------------------------------------------------- //Read CPU time stamp //--------------------------------------------------------------------------- __int64 CWaveIn::CPUTimeStamp(void) { asm { xor eax,eax xor ebx,ebx xor ecx,ecx xor edx,edx cpuid rdtsc } } //--------------------------------------------------------------------------- The instruction rdtsc reads the CPU time stamp and returns its value in register eax and edx. So you could do the folowing ULONGLONG qwStartTime; ULONGLONG qwStopTime; qwStartTime=CPUTimeStamp(); //Place code here... qwStopTime=CPUTimeStamp(); //Time? qwStopTime-qwStartTime; Succes :)