Are there some good ways to know how a function performs in C? I would like to, for example compare my own function to a library function.
Generally, manual performance testing can be done by checking the number of open connections - active sessions, database connections, amount of CPU time, amount of memory being used etc. This is the amount of performance checking that could be done by a human.
You need high-resolution timers.
On Linux, gettimeofday()
is a decent choice, it gives you microsecond resolution. On Windows, QueryPerformanceCounter()
is typical. Make sure you run your function many times, to get stable readings.
Quick sample, for Linux:
struct timeval t0, t1;
unsigned int i;
gettimeofday(&t0, NULL);
for(i = 0; i < 100000; i++)
function_to_measure();
gettimeofday(&t1, NULL);
printf("Did %u calls in %.2g seconds\n", i, t1.tv_sec - t0.tv_sec + 1E-6 * (t1.tv_usec - t0.tv_usec));
You would of course adjust the count (100,000) to match the performance of the function. It's best if the function really takes a while to run, otherwise the loop and/or the function call overhead might dominate.
The open-source Callgrind profiler (for Linux) is a really awesome way to measure performance. Coupled with KCacheGrind, you get really great visualizations of where your time is spent.
Callgrind is part of Valgrind.
Hello i will give you an example and explain it:
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start_clk = clock();
/*
put any code here
*/
printf("Processor time used by program: %lg sec.\n", \
(clock() - start_clk) / (long double) CLOCKS_PER_SEC);
return 0;
}
output: Processor time used by program: 4.94066e-324 sec.
time.h:
declares clock_t which is an arithmetic(you can do math on this value like i do in the example) time value. basically put any code where the comment is.
CLOCKS_PER_SEC is a macro declared in time.h, use it as the denominator to convert the value into seconds.
it is essential to cast it to long double for two reasons:
Run it (them) several million times (each) and measure the time it takes.
The one that completes faster is the better performant.
gprof can help :)
Here's the result of gprof when I run a program of mine for 10 seconds (function names changed)
Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 60.29 8.68 8.68 115471546 0.00 0.00 workalot 39.22 14.32 5.64 46 122.70 311.32 work_b 0.49 14.39 0.07 inlined 0.07 14.40 0.01 46 0.22 0.22 work_c 0.00 14.40 0.00 460 0.00 0.00 find_minimum 0.00 14.40 0.00 460 0.00 0.00 feedback 0.00 14.40 0.00 46 0.00 0.00 work_a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With