Code Profiling for time in C++

Code profiling is a very important aspect of programming. First you must be wondering what is code profiling. You can always google it but in simple words “code profiling” is just measuring of the resources used by your program or small sections of your program.

Here I will be talking about code profiling for “time”. Dynamically measuring your code for how much time it takes for different input sets is of keen importance when you have to optimise your code. There are many ways you can achieve this,

  • there are many unix tools available to do the job for you.
    • time – just type time while calling your executable file.
      eg: time ./a.out
    • sysstat (to install just type “sudo apt-get install sysstat”) – It has many tools available to check the resources used up in the running processes.
      While your program is running, you can check up the resources used up your program by the commands “iostat -i”, “iostat -c”, “iostat -dx”. if networking is involved you can use “netstat -i”, “netstat -s”. To check the memory usage, i.e., free memory and used memory and memory swapped etc., you can type “free -m”.
    • callgrind – you can download this tool to profile your code.
  • The simple way is to put a small code inside your code to measure the time. Here I’ll tell you how to do that using a small library I have written.

Its use as simple as writing:

int func() {
    timeit s(“func()”);
    // your code here.

It is based on a simple concept that when a object goes out of its scope, its destructor is called. So to time a code snippet, you just have to make an object of the type “timeit” having the same scope as the code snippet. In the above example it will print to the standard error:

           func()       3.21554ms

 The object simply times the call between its construction and destruction. If its difficult to maintain the scope of the object by using curly brackets, then you can also use the “new” and “delete” to manually set the scope. The code is rather simple and here it goes:

#include <iostream>
#include <ctime>

class timeit {
    char const *name;
    timespec t_start;

        timeit(char const *temp): name(temp) {
            clock_gettime(CLOCK_REALTIME, &t_start);
        ~timeit() {
            timespec t_end;
            clock_gettime(CLOCK_REALTIME, &t_end);
            double dur = 1e3 * (t_end.tv_sec - t_start.tv_sec) + 1e-6 * (t_end.tv_nsec - t_start.tv_nsec);
            std::cerr << name << '\t' << std::fixed << dur << " ms\n";

Just include this this code in your program and timeit 😉

5 responses to “Code Profiling for time in C++

  1. Well, if I want to include something in the code itself, I’ll probably use time_t start; and time_t end;
    With this I can time individual portions of my code too, for eg. :
    time_t start = time(NULL);

    Code Block 1

    time_t end1 = time(NULL);
    //time taken to run block 1 = end1-start

    Code block 2

    time_t end2 = time(NULL);
    time taken to run block 2 = end2 – end1
    total time taken till now = end2 – start

    This gives me more freedom to check times at various levels by just inserting this statement wherever i want.
    Just an additional method though! 🙂

    PS. The only downside is that it returns seconds (not ms), so if your code is too fast, you wont see the difference. But then, maybe you dont even need to have a look at time if its faster than a sec! 😛
    and if you still want an accurate measure (highly accurate), use clock ticks! clock_t (returned by clock func) gives you the no of clock cycles since program execution began. Thats extremely accurate, and if you still want time, just divide this by no of clocks/sec (theres a macro for this too)

    1. Well, yes you can do that. But including “timeit” class and just inserting one line of code in your program is just more elegant. Elegance is the crux of it.
      As I said, there are already cooked up tools available on the net, and the same thing can be achieved in many different ways. So doing it in the best way is the whole point right??

Leave a Reply

Your email address will not be published. Required fields are marked *