| | |
calculate the execution time of a program
![]() |
>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:
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:
C Syntax (Toggle Plain Text)
#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);
I'm here to prove you wrong.
Is this like something you are thinking about?
[php]
// 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;
}
}
}
}
[/php]
Note that a tick may not be exactly a millisecond.
[php]
// 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;
}
}
}
}
[/php]
Note that a tick may not be exactly a millisecond.
May 'the Google' be with you!
•
•
Join Date: Oct 2004
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
C Syntax (Toggle Plain Text)
#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);
•
•
•
•
Originally Posted by vegaseat
Note that a tick may not be exactly a millisecond.
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.
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'm here to prove you wrong.
>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.
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- program profiling (C)
- calculate the execution time of a program in java (Java)
- how to calculate execution time (C++)
- Problem in measuring program execution time with c (C)
Other Threads in the C Forum
- Previous Thread: Why perror?
- Next Thread: Multiple output Handles
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






