448,574 Members | 1,205 Online
Need help? Post your question and get tips & solutions from a community of 448,574 IT Pros & Developers. It's quick & easy.

measuring execution time for C++ program

 P: 6 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 Expand|Select|Wrap|Line Numbers double execClock(clock_t c1,clock_t c2)         {             double t=clock1-clock2;             double tim=(t*10)/CLOCKS_PER_SEC;             return tim;         }   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 Expand|Select|Wrap|Line Numbers     int fib(int n)          {             if (n <= 1)                return n;             else                return fib(n-1) + fib (n-2);          }   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 Sep 29 '07 #1
6 Replies

 Expert 100+ P: 1,764 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 Expand|Select|Wrap|Line Numbers double execClock(clock_t c1,clock_t c2)         {             double t=clock1-clock2;             double tim=(t*10)/CLOCKS_PER_SEC;             return tim;         }   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 Expand|Select|Wrap|Line Numbers     int fib(int n)          {             if (n <= 1)                return n;             else                return fib(n-1) + fib (n-2);          }   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 QueryPerformanceCounter and QueryPerformanceFrequency which are defined in windows.h. You use it like: Expand|Select|Wrap|Line Numbers LARGE_INTEGER lFreq; LARGE_INTEGER lTime1; LARGE_INTEGER lTime2; long diffTicks; float diffSecs;   QueryPerformanceCounter(&lFreq); QueryPerformanceFrequency(&lTime1); //function QueryPerformanceFrequency.(&lTime2); diffTicks=lTime1.QuadPart-lTime2.QuadPart;//ticks passed diifSecs=(diffTicks/lFreq.QuadPart);//secs passed. Savage Sep 29 '07 #2

 P: 6 Thanks for reply but it is producing no result; 0 ticks and 0 seconds for each n i.e. 4,8,16, 32 Expand|Select|Wrap|Line Numbers   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)                   {                    QueryPerformanceCounter(&lFreq);                    QueryPerformanceFrequency(&lTime1);                    cout<> quit;             };   ===========================================  int fib(int n)          {             if (n <= 1)                return n;             else                return fib(n-1) + fib (n-2);          }   Output is zero ticks and zero seconds for each computed number Jianyi Sep 29 '07 #3

 Expert 100+ P: 1,764 My bad,wher you have QueryPerformanceFrequency put QueryPerformanceCounter and vise versa.Also subtract time2 from time 1 not vise versa. Savage Sep 29 '07 #4

 P: 6 Though i have got time2 - time1 but notgetting about function calls. Do you mean this way Expand|Select|Wrap|Line Numbers                    QueryPerformanceFrequency(&lTime1);                    QueryPerformanceCounter(&lFreq);                    cout<

 Expert 100+ P: 1,764 Though i have got time2 - time1 but notgetting about function calls. Do you mean this way Expand|Select|Wrap|Line Numbers                    QueryPerformanceFrequency(&lTime1);                    QueryPerformanceCounter(&lFreq);                    cout<

 P: 6 Thank you Now it is fine Jianyi Oct 1 '07 #7