Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Speed again

Speed again

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmstestingbeta-testingperformancehelp
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H hint_54

    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

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #2

    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 using GetTickCounter()... this is the best way to know.


    TOXCCT >>> GEII power
    [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

    H 2 Replies Last reply
    0
    • T toxcct

      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 using GetTickCounter()... this is the best way to know.


      TOXCCT >>> GEII power
      [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

      H Offline
      H Offline
      hint_54
      wrote on last edited by
      #3

      That was it ;) Thanks hint_54

      1 Reply Last reply
      0
      • T toxcct

        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 using GetTickCounter()... this is the best way to know.


        TOXCCT >>> GEII power
        [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

        H Offline
        H Offline
        hint_54
        wrote on last edited by
        #4

        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

        D N 2 Replies Last reply
        0
        • H 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

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #5

          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

          H 1 Reply Last reply
          0
          • D David Crow

            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

            H Offline
            H Offline
            hint_54
            wrote on last edited by
            #6

            Isnt there some other function more precise than this one? regards hint_54

            D 1 Reply Last reply
            0
            • H hint_54

              Isnt there some other function more precise than this one? regards hint_54

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #7

              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

              1 Reply Last reply
              0
              • H 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

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #8

                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.

                H 1 Reply Last reply
                0
                • N Nemanja Trifunovic

                  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.

                  H Offline
                  H Offline
                  hint_54
                  wrote on last edited by
                  #9

                  No. _Debug. regards hint_54

                  1 Reply Last reply
                  0
                  • H hint_54

                    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

                    J Offline
                    J Offline
                    Jim Crafton
                    wrote on last edited by
                    #10

                    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!

                    1 Reply Last reply
                    0
                    • H hint_54

                      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

                      R Offline
                      R Offline
                      Remco Hoogenboezem
                      wrote on last edited by
                      #11

                      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 :)

                      H 1 Reply Last reply
                      0
                      • R Remco Hoogenboezem

                        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 :)

                        H Offline
                        H Offline
                        hint_54
                        wrote on last edited by
                        #12

                        hmmmm... I like that. Thanks ;) hint_54

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups