473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

measuring execution time for C++ program

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
  1. double execClock(clock_t c1,clock_t c2)
  2.         {
  3.             double t=clock1-clock2;
  4.             double tim=(t*10)/CLOCKS_PER_SEC;
  5.             return tim;
  6.         }
  7.  
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
  1.     int fib(int n)
  2.          {
  3.             if (n <= 1)
  4.                return n;
  5.             else
  6.                return fib(n-1) + fib (n-2);
  7.          }
  8.  
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 16736
Savage
1,764 Expert 1GB
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
  1. double execClock(clock_t c1,clock_t c2)
  2.         {
  3.             double t=clock1-clock2;
  4.             double tim=(t*10)/CLOCKS_PER_SEC;
  5.             return tim;
  6.         }
  7.  
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
  1.     int fib(int n)
  2.          {
  3.             if (n <= 1)
  4.                return n;
  5.             else
  6.                return fib(n-1) + fib (n-2);
  7.          }
  8.  
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
  1. LARGE_INTEGER lFreq;
  2. LARGE_INTEGER lTime1;
  3. LARGE_INTEGER lTime2;
  4. long diffTicks;
  5. float diffSecs;
  6.  
  7. QueryPerformanceCounter(&lFreq);
  8. QueryPerformanceFrequency(&lTime1);
  9. //function
  10. QueryPerformanceFrequency.(&lTime2);
  11. diffTicks=lTime1.QuadPart-lTime2.QuadPart;//ticks passed
  12. diifSecs=(diffTicks/lFreq.QuadPart);//secs passed.
Savage
Sep 29 '07 #2
jainyi
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
  1.   while (quit != 'x')
  2.             {
  3.                 LARGE_INTEGER lFreq;
  4.                 LARGE_INTEGER lTime1;
  5.                 LARGE_INTEGER lTime2; 
  6.                 long diffTicks;
  7.                 float diffSecs;
  8.                 int i;
  9.                 for(i=4;i<=66;i=i*2)
  10.                   {
  11.                    QueryPerformanceCounter(&lFreq);
  12.                    QueryPerformanceFrequency(&lTime1);
  13.                    cout<<fib(i);
  14.                    QueryPerformanceFrequency(&lTime2);
  15.                    diffTicks=lTime1.QuadPart-lTime2.QuadPart;//ticks passed
  16.                    diffSecs=(diffTicks/lFreq.QuadPart);//secs passed.
  17.                    cout<<" With ticks: " <<diffTicks<<" and seconds: "<<diffSecs<<endl;
  18.                   }       
  19.                 cout << endl << " Press x to quit "<<endl;
  20.                 cin >> quit;
  21.             };
  22.  
  23. ===========================================
  24.  int fib(int n)
  25.          {
  26.             if (n <= 1)
  27.                return n;
  28.             else
  29.                return fib(n-1) + fib (n-2);
  30.          }
  31.  
Output is zero ticks and zero seconds for each computed number

Jianyi
Sep 29 '07 #3
Savage
1,764 Expert 1GB
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
jainyi
6
Though i have got time2 - time1 but notgetting about function calls. Do you mean this way

Expand|Select|Wrap|Line Numbers
  1.                    QueryPerformanceFrequency(&lTime1);
  2.                    QueryPerformanceCounter(&lFreq);
  3.                    cout<<fib(i);
  4.                    QueryPerformanceFrequency(&lTime2);
  5.                    diffTicks=lTime2.QuadPart-lTime1.QuadPart;//ticks passed
  6.                    diffSecs=(diffTicks/lFreq.QuadPart);//secs passed.
  7.  

Jianyi
Sep 30 '07 #5
Savage
1,764 Expert 1GB
Though i have got time2 - time1 but notgetting about function calls. Do you mean this way

Expand|Select|Wrap|Line Numbers
  1.                    QueryPerformanceFrequency(&lTime1);
  2.                    QueryPerformanceCounter(&lFreq);
  3.                    cout<<fib(i);
  4.                    QueryPerformanceFrequency(&lTime2);
  5.                    diffTicks=lTime2.QuadPart-lTime1.QuadPart;//ticks passed
  6.                    diffSecs=(diffTicks/lFreq.QuadPart);//secs passed.
  7.  

Jianyi
No,like this:
Expand|Select|Wrap|Line Numbers
  1. QueryPerformanceFrequency(&lFreq);
  2. QueryPerformanceCounter(&lTime1);
  3. //function
  4. QueryPerformanceCounter(&lTime2);
  5. .
  6. .
  7. .
  8. .
Sep 30 '07 #6
jainyi
6
Thank you

Now it is fine

Jianyi
Oct 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Johannes Lebek | last post by:
Hi there, lately, I experienced a strange thing on my DB2 V8.1 on Windows: Some queries took a very long time. A snapshot discovered the following: Number of executions = 47...
7
by: Tim Quon | last post by:
Hi Is there any function to get the current time so I can calculate the execution time of my code? What all is in the time.h and sys/times.h? Thanks Tim
27
by: vishnu mahendra | last post by:
cah you please tell me how to find the execution time of a program in c. thank you in advance, vishnu
38
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my...
3
by: vashwath | last post by:
Hi all, I have written program for calculating the execution time of a function.Any critics (on the method of calculating execution time) are welcome. #include <stdio.h> #include <time.h>...
6
by: Kameswari | last post by:
Hi, I require code for caluculating the execution time of C program .Can anybody help regarding this. Regards, Kameswari
20
by: upperclass | last post by:
Hi, I'm trying to find a decent way of measuring the running time of a long-running program. Say, the running time ranges from several seconds to more than a day. The standard clock()...
25
by: Umesh | last post by:
i want to calculate the time required to execute a program. Also i want to calcute the time remaining for the execution of the program. how can i do that? pl mention some good websites for...
8
by: Magesh | last post by:
Consider a block, fncall( ); /* tells me the current millisecond or something like that: time-1 */ {/* block of code for which I need to know the exec time ... ... ... } fncall( ); /* tells...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.