-
benchmarking
I guess this is a very broad question, but I'm looking for information on how I can benchmark my code. How do you guys check to see how long it takes to execute a given block of statements? How do you determine that code written one way is faster than it is another way? Any general advice and/or tips would be appreciated. Thanks.
-
Get the time before you start the block of code, and get the time after you've finished the block of code. Subtract A from B, and there you have it. The method of getting times and their differences depends on the operating system you are using though. On a windows computer, one function you can use is timeGetTime().
-
Code:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t start,end;
char string[20];
double dif;
time (&start);
printf ("Insert string : ");
scanf("%s", &string);
time (&end);
dif = difftime (end,start);
printf ("\nFor inserting string you need %2lf seconds\n", dif );
return 0;
}
-
That example will only give you time in seconds, which wouldn't be very effective for benchmarking. You'll want to measure it in milliseconds.
Using ftime would be much more effective.
-
You can use gprof, which is available in Linux. you can goggle gprof to learn how to use, which is quite easy.
It basically records the time taken by each of the functions in your problem
-
For windows, you buy/find a good profiling tool, in unix you use one that is part of the standard toolset, like gprof or prof. Compile with profiling and debugging (symbol table) enabled.
run the program.
You get output like this (mangled names have an underline _ character in front of them):
For the OS this runs on the $$ signified millicode.
Code:
%Time Seconds Cumsecs #Calls msec/call Name
14.5 0.43 0.43 $$mulI
14.2 0.42 0.85 $$remoI
10.7 0.32 1.17 _mcount
9.6 0.28 1.45 600000 0.00 gnustrstr
7.1 0.21 1.66 900000 0.00 bld
6.2 0.18 1.85 600000 0.00 mstrstr
5.1 0.15 2.00 300000 0.00 bld_good
4.4 0.13 2.13 1212702 0.00 _rand
4.4 0.13 2.26 600000 0.00 _strstr
4.4 0.13 2.39 __strlen20
4.0 0.12 2.51 1200000 0.00 strlen
3.7 0.11 2.62 1 110.00 main
3.3 0.10 2.72 _monstartup
1.7 0.05 2.77 303360 0.00 _strncmp
1.7 0.05 2.82 __strncmp20
0.7 0.02 2.84 __strcpy20
0.1 0.00 2.84 $$divU
0.1 0.00 2.84 __rand_r_posix
-
A similar profiling function is available in MS Visual C++ 6, you enable it in the project options menu and then profile the application to view a breakdown of the functions which use the most time or processing power and various other parameters can be identified. There is no point in optimising some piece of code which isn’t slow to start with or the application spends little time in.
-
does ftime work in windows?
-
Yes ftime will work in Windows I think. I do this code!
Tell me is this good code for ftime! Is that that!
Code:
#include <stdio.h>
#include <dos.h>
#include <sys\timeb.h>
void main(void)
{
struct timeb t_start, t_end;
char array[80]; char ch;
int i;
int t_diff;
ftime(&t_start);
while( (ch = getchar()) != '\n')
{
array[i] = ch;
i++;
}
ftime(&t_end);
t_diff = (int) (1000*(t_end.time - t_start.time)
+ (t_end.millitm - t_start.millitm));
printf("milliseconds %d", t_diff);
return 0;
}