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. Calculating the execution time of a Program

Calculating the execution time of a Program

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelp
10 Posts 4 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.
  • R Offline
    R Offline
    Rane
    wrote on last edited by
    #1

    Hello all, Will u give me way to claculate the execution time of a program(or between any statemants) in Microseconds (in VC++). I was able to get the execution time in Milliseconds anyway by using GetTickCount API. Are there any way to calaculate in Microseconds(programmatically!!!!!!). Problem in above said API is that,when the range goes below 0 (in ms), the Microsecond value is not displayed( but rather 0 is Displayed). I am in immediate need!! Thanks in advace.. :rose: Rane

    D D N 3 Replies Last reply
    0
    • R Rane

      Hello all, Will u give me way to claculate the execution time of a program(or between any statemants) in Microseconds (in VC++). I was able to get the execution time in Milliseconds anyway by using GetTickCount API. Are there any way to calaculate in Microseconds(programmatically!!!!!!). Problem in above said API is that,when the range goes below 0 (in ms), the Microsecond value is not displayed( but rather 0 is Displayed). I am in immediate need!! Thanks in advace.. :rose: Rane

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

      Rane wrote:

      Are there any way to calaculate in Microseconds(programmatically!!!!!!).

      Yes, just multiply milliseconds by 1000. ;)


      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

      "There is no death, only a change of worlds." - Native American Proverb

      R 1 Reply Last reply
      0
      • D David Crow

        Rane wrote:

        Are there any way to calaculate in Microseconds(programmatically!!!!!!).

        Yes, just multiply milliseconds by 1000. ;)


        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

        "There is no death, only a change of worlds." - Native American Proverb

        R Offline
        R Offline
        Rane
        wrote on last edited by
        #3

        But I was not able to get values below 0 ms.(in microseconds range).If it is more ur ans is correct. For Ex: Try finding the execution time of 2 or 3 statements, u will get the answer as 0 ms. Isn't it. Then what is the time spent in executing those statements!!!. Thanks 4 ur Reply:rose: Rane

        D 1 Reply Last reply
        0
        • R Rane

          Hello all, Will u give me way to claculate the execution time of a program(or between any statemants) in Microseconds (in VC++). I was able to get the execution time in Milliseconds anyway by using GetTickCount API. Are there any way to calaculate in Microseconds(programmatically!!!!!!). Problem in above said API is that,when the range goes below 0 (in ms), the Microsecond value is not displayed( but rather 0 is Displayed). I am in immediate need!! Thanks in advace.. :rose: Rane

          D Offline
          D Offline
          Dan McCormick
          wrote on last edited by
          #4

          With a little work, you can get there. You'll need to use the function QueryPerformanceCounter()[^] along with QueryPerformanceFrequency()[^]. Before you start though, you should really read an excellent article here on CP by Joseph Newcomer: Time is the Simplest Thing... to make sure you have a grasp of the many things that can ( and will! ) throw off your timing measurements in the Windows environment. Regards, Dan Remember kids, we're trained professionals.
          Don't try this at home!

          R 2 Replies Last reply
          0
          • R Rane

            But I was not able to get values below 0 ms.(in microseconds range).If it is more ur ans is correct. For Ex: Try finding the execution time of 2 or 3 statements, u will get the answer as 0 ms. Isn't it. Then what is the time spent in executing those statements!!!. Thanks 4 ur Reply:rose: Rane

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

            Read here to make sure you know what you are asking (and why it may not be available).


            "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

            "There is no death, only a change of worlds." - Native American Proverb

            1 Reply Last reply
            0
            • R Rane

              Hello all, Will u give me way to claculate the execution time of a program(or between any statemants) in Microseconds (in VC++). I was able to get the execution time in Milliseconds anyway by using GetTickCount API. Are there any way to calaculate in Microseconds(programmatically!!!!!!). Problem in above said API is that,when the range goes below 0 (in ms), the Microsecond value is not displayed( but rather 0 is Displayed). I am in immediate need!! Thanks in advace.. :rose: Rane

              N Offline
              N Offline
              normanS
              wrote on last edited by
              #6

              Have you tried using the profiler (part of Visual Studio)? Search MSDN for information. I think it gives sub-millisecond resolution (haven't used it for a while, can't remember how to turn it on . . . )

              1 Reply Last reply
              0
              • D Dan McCormick

                With a little work, you can get there. You'll need to use the function QueryPerformanceCounter()[^] along with QueryPerformanceFrequency()[^]. Before you start though, you should really read an excellent article here on CP by Joseph Newcomer: Time is the Simplest Thing... to make sure you have a grasp of the many things that can ( and will! ) throw off your timing measurements in the Windows environment. Regards, Dan Remember kids, we're trained professionals.
                Don't try this at home!

                R Offline
                R Offline
                Rane
                wrote on last edited by
                #7

                Is it possible to use profiler to do this job? If so,how to turn it on & how to include in my Program. Help me Plzzzz :) Rane

                1 Reply Last reply
                0
                • D Dan McCormick

                  With a little work, you can get there. You'll need to use the function QueryPerformanceCounter()[^] along with QueryPerformanceFrequency()[^]. Before you start though, you should really read an excellent article here on CP by Joseph Newcomer: Time is the Simplest Thing... to make sure you have a grasp of the many things that can ( and will! ) throw off your timing measurements in the Windows environment. Regards, Dan Remember kids, we're trained professionals.
                  Don't try this at home!

                  R Offline
                  R Offline
                  Rane
                  wrote on last edited by
                  #8

                  I have tried using the APIs which u have mentioned. It is giving the value in mSec .Is it a correct value or a blurred one? Thanks in Advance. :rose:

                  D 1 Reply Last reply
                  0
                  • R Rane

                    I have tried using the APIs which u have mentioned. It is giving the value in mSec .Is it a correct value or a blurred one? Thanks in Advance. :rose:

                    D Offline
                    D Offline
                    Dan McCormick
                    wrote on last edited by
                    #9

                    Here is a simple console application (developed under VC++ 6 ). It measures the time it takes a 'printf()' statement to execute and displays that time in microseconds.

                    #include <windows.h>
                    #include <stdio.h>

                    int main(int argc, char* argv[])
                    {
                    // retrieve frequency of high-resolution performance counter
                    // exit program if counter is not available on this system

                    LARGE\_INTEGER perfFreq;
                    
                    if( !::QueryPerformanceFrequency(&perfFreq) )
                    {
                        printf( "High Performance Counter not available!\\n" );
                        return -1;
                    }
                    
                    printf( "Performance Counter Frequency:  %I64d ticks / second\\n", perfFreq.QuadPart );
                    printf( "                            or  %e microseconds / tick \\n\\n", 1.0e6 / perfFreq.QuadPart );
                    
                    
                    
                    // determine time needed to execute 'printf()'
                    
                    LARGE\_INTEGER perfStartTime;
                    LARGE\_INTEGER perfEndTime;
                    
                    ::QueryPerformanceCounter( &perfStartTime );
                    
                    printf( "\\n(\* This is the statement that I am timing \*)\\n\\n\\n" );
                    
                    ::QueryPerformanceCounter( &perfEndTime );
                    
                    
                    // report measured execution time
                    
                    printf( "Measured Time:  %I64d ticks\\n", perfEndTime.QuadPart - perfStartTime.QuadPart );
                    printf( "            or  %f microseconds\\n\\n\\n", 1.0e6 \* ( perfEndTime.QuadPart - perfStartTime.QuadPart ) / perfFreq.QuadPart );
                    return 0;
                    

                    }

                    On my machine the output produced is:

                    Performance Counter Frequency: 3579545 ticks / second
                    or 2.793651e-001 microseconds / tick

                    (* This is the statement that I am timing *)

                    Measured Time: 80 ticks
                    or 22.349209 microseconds

                    Dan Remember kids, we're trained professionals.
                    Don't try this at home!

                    R 1 Reply Last reply
                    0
                    • D Dan McCormick

                      Here is a simple console application (developed under VC++ 6 ). It measures the time it takes a 'printf()' statement to execute and displays that time in microseconds.

                      #include <windows.h>
                      #include <stdio.h>

                      int main(int argc, char* argv[])
                      {
                      // retrieve frequency of high-resolution performance counter
                      // exit program if counter is not available on this system

                      LARGE\_INTEGER perfFreq;
                      
                      if( !::QueryPerformanceFrequency(&perfFreq) )
                      {
                          printf( "High Performance Counter not available!\\n" );
                          return -1;
                      }
                      
                      printf( "Performance Counter Frequency:  %I64d ticks / second\\n", perfFreq.QuadPart );
                      printf( "                            or  %e microseconds / tick \\n\\n", 1.0e6 / perfFreq.QuadPart );
                      
                      
                      
                      // determine time needed to execute 'printf()'
                      
                      LARGE\_INTEGER perfStartTime;
                      LARGE\_INTEGER perfEndTime;
                      
                      ::QueryPerformanceCounter( &perfStartTime );
                      
                      printf( "\\n(\* This is the statement that I am timing \*)\\n\\n\\n" );
                      
                      ::QueryPerformanceCounter( &perfEndTime );
                      
                      
                      // report measured execution time
                      
                      printf( "Measured Time:  %I64d ticks\\n", perfEndTime.QuadPart - perfStartTime.QuadPart );
                      printf( "            or  %f microseconds\\n\\n\\n", 1.0e6 \* ( perfEndTime.QuadPart - perfStartTime.QuadPart ) / perfFreq.QuadPart );
                      return 0;
                      

                      }

                      On my machine the output produced is:

                      Performance Counter Frequency: 3579545 ticks / second
                      or 2.793651e-001 microseconds / tick

                      (* This is the statement that I am timing *)

                      Measured Time: 80 ticks
                      or 22.349209 microseconds

                      Dan Remember kids, we're trained professionals.
                      Don't try this at home!

                      R Offline
                      R Offline
                      Rane
                      wrote on last edited by
                      #10

                      Thanks for your Information! :) Rane

                      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