Home > C, C++ > Measure Execution Time for a C/C++ Function

Measure Execution Time for a C/C++ Function


This is my snippet for measuring execution time for a C/C++ function.

typedef unsigned long long timestamp_t;

struct time_measure {
    timestamp_t     start;
    timestamp_t     stop;
    double      result;
};

timestamp_t get_timestamp()
{
    struct timeval now;
    gettimeofday(&now, NULL);
    return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}

void update_time(struct time_measure *t)
{
    t->result = (t->stop - t->start) / 1000000.0L;
}

Cheers,
Pete Houston

Advertisements
Categories: C, C++ Tags: ,
  1. November 8, 2012 at 1:48 pm

    Attractive part of content. I simply stumbled upon your website and in accession capital to claim that I acquire in fact loved account your weblog posts. Any way I will be subscribing in your feeds or even I achievement you get entry to consistently quickly.

  2. February 6, 2013 at 1:21 pm

    Thanks $author for this post, I like how you have presented
    it and could think about posting something comparable myself.

  1. No trackbacks yet.

Leave a Reply

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

%d bloggers like this: