Speed of Linux Time System Calls

I wrote a little program to benchmark the gettimeofday and clock_gettime system calls on Linux. It’s included at the  bottom of this post. Here are the results of running on 32 and 64 bit systems. The 64 bit benchmark is courtesy of Klez (see his comment).

32 Bit 64 bit
gettimeofdays/s 750000 13340000
clock_gettimes/s 750000 13450000
processor model Intel Pentium Dual T2390 AMD Athlon 7750 Dual
processor speed 800 MHz 1350 MHz
cache size 1024 KB 512 KB
Linux kernel version 2.6.32-24-generic #39-Ubuntu SMP i686 2.6.32-25-generic #44-Ubuntu SMP x86_64

You see such very different numbers on a 64 bit machine with a recent build of Linux, because those systems support virtual system calls for gettimeofday and, in more recent kernels, clock_gettime. The Linux FAQ for clock_gettime(CLOCK_REALTIME) gives a good explanation of this.

To compile the benchmark, just:

cc test-time-bench.c -lrt -o test-time-bench

The -lrt is needed to include clock_gettime(). If you run the resulting program ./test-time-bench with the -g option, you’ll get the speed of gettimeofday in calls/second on one core of your machine. With -c, its clock_gettime calls/second.

Here’s the code:

#include 
#include 
#include 
#include <sys/time.h>
#include 

int
main(int argc, char ** argv)
{
    if (argc == 1) {
        printf("usage: test-time-bench.t -g [gettimeofday] |"
               " -c [clock_gettime]\n");
        return 0;
    }

    if (argv[1][1] == 'g') {
        struct timeval tv0;
        struct timeval tv1;
        struct timeval tv2;

        gettimeofday(&tv0, NULL);

        do {
            gettimeofday(&tv1, NULL);
        } while (memcmp(&tv0, &tv1, sizeof(tv0)) == 0);

        unsigned long i;

        for (i = 0; ; i++) {
            gettimeofday(&tv2, NULL);

            if ((tv1.tv_sec < tv2.tv_sec) && (tv1.tv_usec <= tv2.tv_usec)) {
            //    fprintf(stderr, "tv1=(%lu,%lu), tv2=(%lu,%lu)\n",
            //            tv1.tv_sec, tv1.tv_usec, tv2.tv_sec, tv2.tv_usec);
                break;
            }
        }

        printf("%lu,%lu: Called gettimeofday %lu times in 1 second\n",
               tv2.tv_sec, tv2.tv_usec, i);
    }
    else if (argv[1][1] == 'c') {
        struct timespec tv0;
        struct timespec tv1;
        struct timespec tv2;

        clock_gettime(CLOCK_REALTIME, &tv0);

        do {
            clock_gettime(CLOCK_REALTIME, &tv1);
        } while (memcmp(&tv0, &tv1, sizeof(tv0)) == 0);

        unsigned long i;

        for (i = 0; ; i++) {
            clock_gettime(CLOCK_REALTIME, &tv2);

            if ((tv1.tv_sec < tv2.tv_sec) && (tv1.tv_nsec <= tv2.tv_nsec)) {
            //    fprintf(stderr, "tv1=(%lu,%lu), tv2=(%lu,%lu)\n",
            //            tv1.tv_sec, tv1.tv_nsec, tv2.tv_sec, tv2.tv_nsec);
                break;
            }
         }

         printf("%lu,%lu: Called clock_gettime %lu times in 1 second\n",
                tv2.tv_sec, tv2.tv_nsec, i);
    }

    return 0;
}
Advertisements

About jimbelton

I'm a software developer, and a writer of both fiction and non-fiction, and I blog about movies, books, and philosophy. My interest in religious philosophy and the search for the truth inspires much of my writing.
This entry was posted in programming and tagged , , , , , . Bookmark the permalink.

2 Responses to Speed of Linux Time System Calls

  1. klez says:

    I ran each test 3 times, it looks like the ‘-g’ option is very slightly slower. Also my AMD64 beats Jim’s Pentium like a drum!

    [dave@flea tmp]$ cc test-time-bench.c -lrt -o test-time-bench
    [dave@flea tmp]$ ./test-time-bench -c
    1287713243,828583414: Called clock_gettime 13459080 times in 1 second
    [dave@flea tmp]$
    [dave@flea tmp]$ ./test-time-bench -g
    1287713292,687389: Called gettimeofday 13348094 times in 1 second
    [dave@flea tmp]$
    [dave@flea tmp]$ ./test-time-bench -c
    1287713303,738556267: Called clock_gettime 13430958 times in 1 second
    [dave@flea tmp]$
    [dave@flea tmp]$ ./test-time-bench -g
    1287713306,648527: Called gettimeofday 13196742 times in 1 second
    [dave@flea tmp]$
    [dave@flea tmp]$ ./test-time-bench -c
    1287713309,288550647: Called clock_gettime 13394864 times in 1 second
    [dave@flea tmp]$
    [dave@flea tmp]$ ./test-time-bench -g
    1287713312,168594: Called gettimeofday 13342874 times in 1 second
    [dave@flea tmp]$
    [dave@flea tmp]$ uname -a
    Linux flea 2.6.32-25-generic #44-Ubuntu SMP Fri Sep 17 20:05:27 UTC 2010 x86_64 GNU/Linux
    [dave@flea tmp]$ cat /proc/cpuinfo
    processor : 0
    vendor_id : AuthenticAMD
    cpu family : 16
    model : 2
    model name : AMD Athlon(tm) 7750 Dual-Core Processor
    stepping : 3
    cpu MHz : 1350.000
    cache size : 512 KB
    physical id : 0
    siblings : 2
    core id : 0
    cpu cores : 2

  2. I have seen a lot of useful things on your web site
    about pcs. However, I’ve got the view that laptops are still not quite powerful adequately to be a good
    selection if you typically do things that require a great deal of power,
    for example video croping and editing. But for net surfing,
    statement processing, and majority of other frequent computer work they are fine,
    provided you never mind the little screen size. Thank you for sharing your thinking.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s