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 -
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 -
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
6 16736
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 -
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 -
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: - 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
Thanks for reply
but it is producing no result; 0 ticks and 0 seconds for each n i.e. 4,8,16, 32 -
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<<fib(i);
-
QueryPerformanceFrequency(&lTime2);
-
diffTicks=lTime1.QuadPart-lTime2.QuadPart;//ticks passed
-
diffSecs=(diffTicks/lFreq.QuadPart);//secs passed.
-
cout<<" With ticks: " <<diffTicks<<" and seconds: "<<diffSecs<<endl;
-
}
-
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);
-
}
-
Output is zero ticks and zero seconds for each computed number
Jianyi
My bad,wher you have QueryPerformanceFrequency put QueryPerformanceCounter and vise versa.Also subtract time2 from time 1 not vise versa.
Savage
Though i have got time2 - time1 but notgetting about function calls. Do you mean this way -
QueryPerformanceFrequency(&lTime1);
-
QueryPerformanceCounter(&lFreq);
-
cout<<fib(i);
-
QueryPerformanceFrequency(&lTime2);
-
diffTicks=lTime2.QuadPart-lTime1.QuadPart;//ticks passed
-
diffSecs=(diffTicks/lFreq.QuadPart);//secs passed.
-
Jianyi
Though i have got time2 - time1 but notgetting about function calls. Do you mean this way -
QueryPerformanceFrequency(&lTime1);
-
QueryPerformanceCounter(&lFreq);
-
cout<<fib(i);
-
QueryPerformanceFrequency(&lTime2);
-
diffTicks=lTime2.QuadPart-lTime1.QuadPart;//ticks passed
-
diffSecs=(diffTicks/lFreq.QuadPart);//secs passed.
-
Jianyi
No,like this: -
QueryPerformanceFrequency(&lFreq);
-
QueryPerformanceCounter(&lTime1);
-
//function
-
QueryPerformanceCounter(&lTime2);
-
.
-
.
-
.
-
.
Thank you
Now it is fine
Jianyi
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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
|
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...
|
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>...
|
by: Kameswari |
last post by:
Hi,
I require code for caluculating the execution time of C program .Can anybody help regarding this.
Regards,
Kameswari
|
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()...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |