I'm using gcc for my c programmes. How can I check which method is faster (suppose that I write a code to swap two numbers and I rewrote the same code using bit operator), is there any tool in linux to check the time,performance and space?
man gprof should help.
But remember, if you use profiler you should test large number of loops. And you should do it with caching effect counted so at least this should be performed on large enough memory area data with random (but the same) order. Use sranddom() / random() for this.
Minimal setup:
- write 'test bed' which loops different methods through the same input (large enough loops).
- compile / link your modules with GNU compiler -pg option.
- run. You should obtain profile data file (gmon.out usually).
- man gprof -> gprof [options] -> produce reports you need -> see what you really need.
In the simple case you describe i'd use this
$ vi test.c
$ make test
cc test.c -o test
$ time ./test
real 0m1.001s
user 0m0.001s
sys 0m0.000s
When comparing the full execution of several components i'd use the gprof method outlined by Roman.
It all depends on the situation. But 9 times out of 10 the time approach is sufficient to me. But i suppose when working with threads multiple processes and GUI code the situation would be different. That is however not my field of expertise.
There is a tool called Valgrind (wikipedia) that i recommend you too have a look at.
There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail.
If you need to monitor time within your program, or in a test code or log it, you can simply use times() or clock() .
http://www.gnu.org/software/libc/manual/html%5Fnode/Processor-And-CPU-Time.html
For finding the time taken by your code, you could use something like this:
clock_t start, end;
start = clock();
< your block(s) of code here>
end = clock();
printf("Time taken %lf\n", (double) (end-start) / CLOCKS_PER_SEC);
It gives the time taken in seconds. Uses time.h
For finding the memory consumptions, you can check the relevant field using 'top' when you run the process.
The simplest way to do such a query is this:
for (i = 100000000; --i >= 0; ){
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
swap(&a, &b);
}
where swap
is the function you want to time, this code runs it 10^9 times.
Then just time it with your watch. The number of seconds it takes translates to nanoseconds.
With a function as simple as a number swap, the easiest way to compare memory usage and speed might be to look at the code in a debugger. When you can see the optimized assembly alongside the original C, you should be able to count assembly operations for each (can't imagine a number swap being more than a handful of assembly lines) and get a good handle on which method is faster, and look at the number of registers involved in each function to decide which method uses more memory. You wouldn't want to do this on large functions, however.