Measure Execution Time

Aug 16, 2012·
Julio Batista Silva
Julio Batista Silva
· 2 min read
blog

In ZSH:

julio@acer ~> time ./programa
  Hello World!
  ./programa  1.54s user 0.03s system 97% cpu 1.611 total
julio@acer ~> type -a time
  time is a reserved word
julio@acer ~> which time
  time: shell reserved word
julio@acer ~> sudo pacman -S time
julio@acer ~> /usr/bin/time ./programa
  Hello World!
  1.39user 0.00system 0:01.41elapsed 99%CPU (0avgtext+0avgdata 8940maxresident)k
  0inputs+0outputs (0major+758minor)pagefaults 0swaps
julio@acer ~> /usr/bin/time -v ./programa
  Hello World!
      Command being timed: "./programa"
      User time (seconds): 1.57
      System time (seconds): 0.05
      Percent of CPU this job got: 99%
      Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.63
      Average shared text size (kbytes): 0
      Average unshared data size (kbytes): 0
      Average stack size (kbytes): 0
      Average total size (kbytes): 0
      Maximum resident set size (kbytes): 8936
      Average resident set size (kbytes): 0
      Major (requiring I/O) page faults: 0
      Minor (reclaiming a frame) page faults: 758
      Voluntary context switches: 1
      Involuntary context switches: 181
      Swaps: 0
      File system inputs: 0
      File system outputs: 3160
      Socket messages sent: 0
      Socket messages received: 0
      Signals delivered: 0
      Page size (bytes): 4096
      Exit status: 0
julio@acer ~> /usr/bin/time -p ./programa
Hello World!
real 1.45
user 1.44
sys 0.00

alias time='/usr/bin/time -p'

C/C++

The precision of time and /usr/bin/time is not very good if you want to do more serious profiling, or if you want to measure the time spent by a specific part of the code rather than by the whole program. For those cases, on Linux and FreeBSD, we can use the clock_gettime function from time.h.

Looking at the file /usr/include/time.h: int clock_gettime(clockid_t clk_id, struct timespec *tp);

CLOCK_REALTIME: The system clock. CLOCK_MONOTONIC: CLOCK_PROCESS_CPUTIME_ID: A timer the CPU provides per process. CLOCK_THREAD_CPUTIME_ID: A timer the CPU provides per thread.

#include <ctime>

timespec diff(timespec start, timespec end)
{
    timespec temp;
    if (end.tv_nsec - start.tv_nsec < 0)
    {
        temp.tv_sec = end.tv_sec - start.tv_sec-1;
        temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
    }
    else
    {
        temp.tv_sec = end.tv_sec - start.tv_sec;
        temp.tv_nsec = end.tv_nsec - start.tv_nsec;
    }
    return temp;
}

int main()
{
    timespec time1, time2;
    clock_gettime(CLOCK_REALTIME, &time1);

    // ... ... ...

    clock_gettime(CLOCK_REALTIME, &time2);

    cout << diff(time1,time2).tv_sec << ":"
            << diff(time1,time2).tv_nsec << endl;

    return 0;
}

This program should be compiled with g++ -lrt -o programa programa.cpp, and the result will look something like this:

julio@acer ~> ./programa
  0:7674929
Julio Batista Silva
Authors
Senior Cloud Developer

I’m a Brazilian computer engineer based in Germany, passionate about tech, science, photography, and languages.

I’ve been programming for about two decades already, exploring everything from mobile apps and web development to machine learning. These days I focus on cloud SRE and data engineering.

I volunteer in the open source and Python communities, helping organize PyCon DE and PyData Berlin, mentoring, and contributing with code and translations.

On my blog, I share Linux tips, setup guides, and personal notes I’ve written for future reference. I hope others find them helpful as well. The content is available in multiple languages.

Browse my gallery for some of my photography.

Away from the keyboard, you’ll find me at concerts, playing clarinet, cycling, scuba diving, or exploring new places, cultures, and cuisines.

Always happy to connect! 🙂

comments powered by Disqus