Hi all,

Suppose if I write two programs of same functionality but with different methods and want to measure the run time efficiency how do i know? I mean to say i want to measure the run time of each code how do i do it? Should i check the efficiency of code upto milliseconds?

It depends upon the system. Linux can handle timing in the nanosecond realm. You need to run both algorithms many times, accumulate the time, and then compare. Just comparing on one run that may only take nano, micro, or milliseconds is not sufficient since other stuff is also taking system time. Your process is not the ONLY process running...

Check if your development environment includes a profiler. You can get a lot of information from a decent profiler. This will help you identify bottlenecks and the profiler will take care of the timing issues for you.

I found this a short time ago and thought it looked interesting enough to test it out... (I only tested it on a MS Windows OS.)

/* cpu_times.h */

found at:


#ifndef FoundOnWeb_CPU_TIMES_H
#define FoundOnWeb_CPU_TIMES_H

/*  Windows */

#ifdef _WIN32
#include <Windows.h>

double get_wall_time()
    LARGE_INTEGER time, freq;
    if (!QueryPerformanceFrequency(&freq))
        /*  Handle error */
        return 0;
    if (!QueryPerformanceCounter(&time))
        /*  Handle error */
        return 0;
    return (double)time.QuadPart / freq.QuadPart;

double get_cpu_time()
    FILETIME a, b, c, d;
    if ( GetProcessTimes( GetCurrentProcess(), &a, &b, &c, &d ) != 0 )
        /*  Returns total user time.
            Can be tweaked to include kernel times as well. */
            (double)(d.dwLowDateTime |
            ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
        /*  Handle error */
        return 0;

/*  Posix/Linux */

#include <sys/time.h>
double get_wall_time()
    struct timeval time;
    if( gettimeofday( &time , NULL ) )
        /*  Handle error */
        return 0;
    return (double)time.tv_sec + (double)time.tv_usec * .000001;
double get_cpu_time()
    return (double)clock() / CLOCKS_PER_SEC;


#endif /* FoundOnWeb_CPU_TIMES_H */

If trying to compare, for example, two sort methods, of a large array of data redords ...

I usually, simply track the clock times, for several runs,

(maybe 5 to 10 runs each, and often in different orders of being run ... also),

and then take an average, (and some times even the sd),

to get a feel for the real running time the process could take.

Something like the code below,
can give you a pretty good estimate of relative runnning times on your 'system', for any processes that take about a second or so (each) to run:

const int MAX_LEN = 1000000;
// ... //

void sort1( int ary[], int size );
void sort2( int ary[], int size );

int load( const char* fname, int ary[], int max_size );
void getCpy( int ary[], int aryCpy[], int size );

// fill a large array of int 

int *ary = new int[MAX_LEN], *aryCpy = new int[MAX_LEN];

int size = load( IN_FILE_NAME, ary, MAX_LEN );
if( size )
    getCpy( ary, aryCpy, size );

    for( int i = 0; i < 3; ++ i )
        double t1 = clock();
        sort1( ary, size );
        double t2 = clock();
        cout << "sort 1 time was: " << (t2-t1)/CLOCKS_PER_SEC << endl;

        getCpy( aryCpy, ary, size );

        t1 = clock();
        sort2( ary, size );
        t2 = clock();
        cout << "sort 2 time was: " << (t2-t1)/CLOCKS_PER_SEC << endl;

        getCpy( aryCpy, ary, size );

just type in the terminal time EXE_NAME and it will give you the runtime..the manpage of time looks like this

TIME(1)                   BSD General Commands Manual                  TIME(1)

     time -- time command execution

     time [-lp] utility

     The time utility executes and times utility.  After the utility finishes, time writes the total time elapsed, the time consumed by system overhead, and the time used to execute utility
     to the standard error stream.  Times are reported in seconds.

     Available options:

     -l      The contents of the rusage structure are printed.

     -p      The output is formatted as specified by IEEE Std 1003.2-1992 (``POSIX.2'').

     Some shells may provide a builtin time command which is similar or identical to this utility.  Consult the builtin(1) manual page.

     The time utility shall exit with one of the following values:

     1-125   An error occurred in the time utility.

     126     The utility was found but could not be invoked.

     127     The utility could not be found.

     Otherwise, the exit status of time shall be that of utility.

     builtin(1), csh(1), getrusage(2)


     The time utility conforms to IEEE Std 1003.2-1992 (``POSIX.2'').

     The granularity of seconds on microprocessors is crude and can result in times being reported for CPU usage which are too large by a second.

BSD                              June 6, 1993                              BSD