Hi,
i would like to calculate the execution time of my program.
is there any function available to enable it??
Store system datetime in variable at start of your procedure.
Display something like datediff(Variable,now()) at the end of your procedure.
I don't know what language your are using so I'm being vague.
>I don't know what language your are using so I'm being vague.
You could assume either C or C++ and probably hit the mark. :rolleyes:
>is there any function available to enable it??
Poor man's profiler:
#include <stdio.h>
#include <time.h>
clock_t start = clock();
/* Code you want timed here */
printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
Is this like something you are thinking about?
// time the bubblesort of an array of random integers
#include <windows.h> // GetTickCount()
#include <stdio.h> // printf(), getchar()
#include <stdlib.h> // srand(), rand()
#define NUM_ITEMS 10000
void bubbleSort(int numbers[], int array_size);
int numbers[NUM_ITEMS];
int main()
{
int k;
long te, ts;
// seed random number generator
srand(GetTickCount());
// fill array with random integers
for (k = 0; k < NUM_ITEMS; k++)
numbers[k] = rand();
// start counter
ts = GetTickCount();
// perform bubble sort on array
bubbleSort(numbers, NUM_ITEMS);
// end counter
te = GetTickCount();
// show the count difference
printf("Bubble sort of 10000 random integers took %d ms\n", te - ts);
getchar(); // wait
return 0;
}
void bubbleSort(int numbers[], int array_size)
{
int finished = 0, i, j, temp;
for (i = (array_size - 1); i >= 0; i--) {
if (finished)
break;
finished = 1;
for (j = 1; j <= i; j++) {
if (numbers[j-1] > numbers[j]) {
finished = 0;
// swap
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
Note that a tick may not be exactly a millisecond.
i am using C.
i will check them out, thanks..
#include <stdio.h> #include <time.h> clock_t start = clock(); /* Code you want timed here */ printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
the code is working fine, except that the time it calculates is a bit lesser than what my stopclock shows.
Note that a tick may not be exactly a millisecond.
is it the case here??
Yes. Clock ticks don't always correspond to any normal measurement of time, so any precise measurement less than a second should be done with a profiler program such as gprof. That's certainly easier than writing your own timing code. Anything that requires accuracy will be rather complicated.
The code I gave you is a good, relatively portable way to compare similar bits of code. For example, if you wanted to test the performance of two functions and compare the results, wallclock time doesn't matter because both tests will use the same time granularity. If you want the exact number of milliseconds (for example) that a piece of code takes, you're SOL with any of the simple methods.
i was just wondering, is there a convertion ratio between the clock ticks and the actual time in milliseconds?
i also understand the clock ticks vary according to the processor, right??
>is there a convertion ratio between the clock ticks and the actual time in milliseconds?
You can calculate that from the clock speed of your processor if you want. But if accurate wall clock timing is important, you should be using a real profiler to begin with instead of trying to hack a timer into your code.
Hey people I have a doubt.I learnt that there cant be a general algorithm to find out whether a given program gets exceuted in finite time or not.Is that so?
Maybe, can you give a better description of what you're talking about?
>is there a convertion ratio between the clock ticks and the actual time in milliseconds?
You can calculate that from the clock speed of your processor if you want. But if accurate wall clock timing is important, you should be using a real profiler to begin with instead of trying to hack a timer into your code.
no, this is good enough for my use,
as i just need a measure of time taken for the whole code as one, and time taken for each of the operation in the code done seperately.
but if there is a conversion rate, it would be interesting to know. thats it.. ;)
hmmmmm...interesting
how to plot in graphs
Here are some codes which you may use to record the time.
#include <time.h>
time_t start, end;
double duration = 0;
time(&start);
// your codes here
time(&end);
duration = difftime(end, start); // time in milliseconds
Here are some codes which you may use to record the time.
#include <time.h> time_t start, end; double duration = 0; time(&start); // your codes here time(&end); duration = difftime(end, start); // time in milliseconds
difftime returns the time difference in seconds. There's no portable way to get millisecond granularity.
Hi,
i would like to calculate the execution time of my program.
is there any function available to enable it??
0
For what it's worth, here's one that's just a few macros:
#include <time.h>
clock_t startm, stopm;
#define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define PRINTTIME printf( "%6.3f seconds used by the processor.", ((double)stopm-startm)/CLOCKS_PER_SEC);
Then just use it with:
main() {
START;
// Do stuff you want to time
STOP;
PRINTTIME;
}
0
For what it's worth, here's one that's just a few macros:#include <time.h>
clock_t startm, stopm;
#define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define PRINTTIME printf( "%6.3f seconds used by the processor.", ((double)stopm-startm)/CLOCKS_PER_SEC);
Then just use it with:main() {
START;
// Do stuff you want to time
STOP;
PRINTTIME;
}
Not a good use of the preprocessor. Let's look at a few reasons why:
Seriously, just put forth a little more effort and write a stopwatch ADT. Macro hell isn't a good place to be.
i am using C language.
And anyone can tell about machine cycles the instructions will take in execution??
it will help me out to optimize the code to run in shorter time.
send any link regards each instruction take howmany cycles and related.
@ Eivnay: Yes. That is a limitation of any Turing style computer.
On timing programs: The early PC's had the timer running off the PIC chip (programmable interrupt controller), using 1/3rd of it's clock sweeps.
That gives decent timing IF you have a DOS or real time operating system, not running anything else.
Windows naturally, is not so good at this. All calls for clock() are prioritized, and taken in turn with other requests.
Recently, Intel has come out with high precision timer hardware, which is in many new boards with Intel chipsets in them. Microsoft was supposed to be supplying the driver, and they wanted their own "multimedia timer" software to be used, instead. The "multimedia timer was a flop. Now MS is supposed to release patches that allow Windows to use the Intel high precision timer, but I have not seen how good it is, personally.
The advantages are two fold: 1) finer resolution given, and 2) very fast set up, allowing multiple times (think of several functions and the program as a whole), to be timed better.
Since the profiling programs rely on this so much, they will undoubtedly be on top of the latest and greatest timing available.
Giving actual machine cycles for a function sounds like Unix/Linux/BSD stuff. Have not seen that in any version of Windows, but I'm new with Windows 7.
hello,
I want to calculate the time simulation of my code VHDL ?
can you help me
how to implement in a c program ! m not clear with yo steps! where to place dis statement in the program
3.time_t start, end;
4.double duration = 0;
6.time(&start);
could you pls explain d program
wat i actually want is! i hav to calculate the execution time of my program
if anybody nos it help me with d codes
This is what I use generally. There are newer timers available with higher resolutions, but this is all I need. I have identified the three lines of code for timing, with #0, #1, #2, and #3. You must have all four lines of code.
#3 line of code using the #define MACRO from time.h. Mine is called "CLOCKS_PER_SEC". That name changes from compiler to compiler. In Turbo C it was called "TICKS_PER_SEC". In Microsoft's compiler they have a slightly different name for it. Be prepared to look it up by reading the short file "time.h", or using the help that came with your compiler (or Google it).
This program is small and takes no measurable time, but it will show time used by a slower program. (It builds a Pascal Triangle, tilted to the left by 45 degrees, in case you're wondering.)
#include <stdio.h>
#include <time.h> //#0
#define SIZE 20
int main(void) {
int r,c,width,depth,size;
unsigned long long int num;
unsigned long long int pascal[SIZE*2+1][SIZE*2+1]={0};
clock_t timer=clock(); //#1
size=SIZE*2+1;
width=size;
depth=SIZE%2?SIZE:SIZE;
for(r=0,num=1;r<size;r++,width--) {
for(c=0;c<width;c++) {
if(r==0 || c==0) {
num=1;
}else {
num=pascal[r][c-1]+pascal[r-1][c];
}
if(size<16)
printf("%4llu ",num);
pascal[r][c]=num;
}
putchar('\n');
}
printf("\ndepth: %d\n",depth);
//The triangle is rotated 45° left for easier creation, so
//printing the answers for each depth, we traverse a diagonal, here.
for(r=0,c=0;r<=depth;r++,c++) {
//print out the center number (largest one), in each row
printf("%d) %llu\n",r,pascal[r][c]);
}
timer=clock()-timer; //#2
printf("Answer is: %llu \n\n",pascal[depth][c-1]);
printf("Elapsed time: %f\n",(double) timer/CLOCKS_PER_SEC); //#3
return 0;
}
Mine is called "CLOCKS_PER_SEC". That name changes from compiler to compiler.
CLOCKS_PER_SEC
is the standard macro. Any compiler claiming to implement standard C must define it. Any other macro for a similar value is not standard. Here's the same technique wrapped up in an opaque library. First, the header:
#ifndef TIMER_H
#define TIMER_H
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef clock_t timer_t;
inline timer_t timer_get(void)
{
return clock();
}
inline double timer_elapsed(timer_t start)
{
return ((double)clock() - start) / CLOCKS_PER_SEC;
}
#ifdef __cplusplus
}
#endif
#endif /* TIMER_H */
And then the implementation file:
#include "timer.h"
#ifndef __cplusplus
extern inline timer_t timer_get(void);
extern inline double timer_elapsed(timer_t start);
#endif
Note that this assumes a compiler supporting at least C99 or that the code is compiled as C++ because it takes advantage of inline functions. Given that you used long long, I'm assuming that's the case. Otherwise your code isn't portable. ;) If you want it to be compatible with C90, you can't use inline and the files would be more traditional, with the header like this:
#ifndef TIMER_H
#define TIMER_H
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef clock_t timer_t;
extern timer_t timer_get(void);
extern double timer_elapsed(timer_t start);
#ifdef __cplusplus
}
#endif
#endif /* TIMER_H */
And the implementation file like this:
#include <time.h>
#include "timer.h"
extern timer_t timer_get(void)
{
return clock();
}
extern double timer_elapsed(timer_t start)
{
return ((double)clock() - start) / CLOCKS_PER_SEC;
}
Using the library doesn't really simplify the code any, but it does hide the implementation from calling code so that you're not tied to a specific method of timing and don't have to repeat the pattern any time you want to time some code:
#include <stdio.h>
#include "timer.h"
#define SIZE 20
int main(void) {
int r,c,width,depth,size;
unsigned long long int num;
unsigned long long int pascal[SIZE*2+1][SIZE*2+1]={0};
timer_t start = timer_get();
double elapsed;
size=SIZE*2+1;
width=size;
depth=SIZE%2?SIZE:SIZE;
for(r=0,num=1;r<size;r++,width--) {
for(c=0;c<width;c++) {
if(r==0 || c==0) {
num=1;
}else {
num=pascal[r][c-1]+pascal[r-1][c];
}
if(size<16)
printf("%4llu ",num);
pascal[r][c]=num;
}
putchar('\n');
}
printf("\ndepth: %d\n",depth);
//The triangle is rotated 45° left for easier creation, so
//printing the answers for each depth, we traverse a diagonal, here.
for(r=0,c=0;r<=depth;r++,c++) {
//print out the center number (largest one), in each row
printf("%d) %llu\n",r,pascal[r][c]);
}
elapsed = timer_elapsed(start);
printf("Answer is: %llu \n\n",pascal[depth][c-1]);
printf("Elapsed time: %f\n", elapsed);
return 0;
}
That program was being used to solve a problem about the number of paths in a grid - which was quite large.
In their forum for the problems, it's customary to give the run time of the solution. Thanks for the timer.h info however. I can definitely use that.
In case you are using GNU Linux use time command in the shell. This utility will give the time taken from the invocation to the end of the program.
The output will be something like this
$time a.out
real 0m0.255s
user 0m0.130s
sys 0m0.030s
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.