measuring execution time for C++ program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jainyi
    New Member
    • Sep 2007
    • 6

    #1

    measuring execution time for C++ program

    Hi,
    I want to calculate execution time for a program computing fibonacci number in C++. I have programmed two functions (one using recursion and other using divide and conquer huristic) to compute fibonnaci number for (n=4,8,16,32,64 ). Now i want to compare these two methods. Though i have used following method to compute execution time but it is not precise as yielding 0 ticker time for n=4,8,16 in both the cases

    [code=c]
    double execClock(clock _t c1,clock_t c2)
    {
    double t=clock1-clock2;
    double tim=(t*10)/CLOCKS_PER_SEC;
    return tim;
    }
    [/code]

    Is there anyother method which is more accurate for lower values of n, i am working in Inet/Windows XP/Dev C++ environment?

    While following using recursive method

    [code=c]
    int fib(int n)
    {
    if (n <= 1)
    return n;
    else
    return fib(n-1) + fib (n-2);
    }
    [/code]

    i can not compute fibonacci number for n=64, just system gets hanged, CPU usage reaches to 97% for the application, it seems it has struk in the computation

    With Regards
    Jianyi
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by jainyi
    Hi,
    I want to calculate execution time for a program computing fibonacci number in C++. I have programmed two functions (one using recursion and other using divide and conquer huristic) to compute fibonnaci number for (n=4,8,16,32,64 ). Now i want to compare these two methods. Though i have used following method to compute execution time but it is not precise as yielding 0 ticker time for n=4,8,16 in both the cases

    [code=c]
    double execClock(clock _t c1,clock_t c2)
    {
    double t=clock1-clock2;
    double tim=(t*10)/CLOCKS_PER_SEC;
    return tim;
    }
    [/code]

    Is there anyother method which is more accurate for lower values of n, i am working in Inet/Windows XP/Dev C++ environment?

    While following using recursive method

    [code=c]
    int fib(int n)
    {
    if (n <= 1)
    return n;
    else
    return fib(n-1) + fib (n-2);
    }
    [/code]

    i can not compute fibonacci number for n=64, just system gets hanged, CPU usage reaches to 97% for the application, it seems it has struk in the computation

    With Regards
    Jianyi
    The most precise method i know is QueryPerformanc eCounter and QueryPerformanc eFrequency which are defined in windows.h.

    You use it like:

    [CODE="cpp"]LARGE_INTEGER lFreq;
    LARGE_INTEGER lTime1;
    LARGE_INTEGER lTime2;
    long diffTicks;
    float diffSecs;

    QueryPerformanc eCounter(&lFreq );
    QueryPerformanc eFrequency(&lTi me1);
    //function
    QueryPerformanc eFrequency.(&lT ime2);
    diffTicks=lTime 1.QuadPart-lTime2.QuadPart ;//ticks passed
    diifSecs=(diffT icks/lFreq.QuadPart) ;//secs passed.[/CODE]

    Savage

    Comment

    • jainyi
      New Member
      • Sep 2007
      • 6

      #3
      Thanks for reply

      but it is producing no result; 0 ticks and 0 seconds for each n i.e. 4,8,16, 32

      [code=c]
      while (quit != 'x')
      {
      LARGE_INTEGER lFreq;
      LARGE_INTEGER lTime1;
      LARGE_INTEGER lTime2;
      long diffTicks;
      float diffSecs;
      int i;
      for(i=4;i<=66;i =i*2)
      {
      QueryPerformanc eCounter(&lFreq );
      QueryPerformanc eFrequency(&lTi me1);
      cout<<fib(i);
      QueryPerformanc eFrequency(&lTi me2);
      diffTicks=lTime 1.QuadPart-lTime2.QuadPart ;//ticks passed
      diffSecs=(diffT icks/lFreq.QuadPart) ;//secs passed.
      cout<<" With ticks: " <<diffTicks<< " and seconds: "<<diffSecs<<en dl;
      }
      cout << endl << " Press x to quit "<<endl;
      cin >> quit;
      };

      =============== =============== =============
      int fib(int n)
      {
      if (n <= 1)
      return n;
      else
      return fib(n-1) + fib (n-2);
      }
      [/code]

      Output is zero ticks and zero seconds for each computed number

      Jianyi

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        My bad,wher you have QueryPerformanc eFrequency put QueryPerformanc eCounter and vise versa.Also subtract time2 from time 1 not vise versa.

        Savage

        Comment

        • jainyi
          New Member
          • Sep 2007
          • 6

          #5
          Though i have got time2 - time1 but notgetting about function calls. Do you mean this way

          [code=c]
          QueryPerformanc eFrequency(&lTi me1);
          QueryPerformanc eCounter(&lFreq );
          cout<<fib(i);
          QueryPerformanc eFrequency(&lTi me2);
          diffTicks=lTime 2.QuadPart-lTime1.QuadPart ;//ticks passed
          diffSecs=(diffT icks/lFreq.QuadPart) ;//secs passed.
          [/code]


          Jianyi

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by jainyi
            Though i have got time2 - time1 but notgetting about function calls. Do you mean this way

            [code=c]
            QueryPerformanc eFrequency(&lTi me1);
            QueryPerformanc eCounter(&lFreq );
            cout<<fib(i);
            QueryPerformanc eFrequency(&lTi me2);
            diffTicks=lTime 2.QuadPart-lTime1.QuadPart ;//ticks passed
            diffSecs=(diffT icks/lFreq.QuadPart) ;//secs passed.
            [/code]


            Jianyi
            No,like this:
            [CODE="cpp"]
            QueryPerformanc eFrequency(&lFr eq);
            QueryPerformanc eCounter(&lTime 1);
            //function
            QueryPerformanc eCounter(&lTime 2);
            .
            .
            .
            .[/CODE]

            Comment

            • jainyi
              New Member
              • Sep 2007
              • 6

              #7
              Thank you

              Now it is fine

              Jianyi

              Comment

              Working...