Thread: need timer

  1. #1
    Registered User
    Join Date
    Oct 2001

    Question need timer

    I have a couple of problems on an project. I have part of it working but need a nudge go get the rest of it working

    I have to time an binary seach and sequential search on an array. I believe I have the array set, but can't get a large enough array to time.

    Also cannot figure out how to time the search.

    Here is what I have on the search.
    Am I doing any of it right?

    int arraySearch[99999];

    for(index = 0; index < 99999; index++)
    arraySearch[index] = num++;

    //Binary search
    while(low <= high)
    int mid = (low + high)/2;

    if(arraySearch[mid] < x)
    low = mid + 1;
    else if(x < arraySearch[mid])
    high = mid - 1;
    return mid;
    //Sequential search
    for(index = 0; index < 99999; index++)
    if(num == arraySearch[index])
    ans = index;

  2. #2
    Prisoner of my own mind
    Join Date
    Aug 2001
    For timing something like this should work.

    #include <time.h>


    time_t start, finish;
    double result, elapsed_time;

    time( &start );

    // insert your code or function call here that you want to time

    time( &finish );

    elapsed_time = difftime( finish, start );
    printf( "\nProgram takes %6.0f seconds.\n", elapsed_time );
    Lead me not into temptation... I can find it myself.

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    If you are using UNIX (I am not too sure if you can do this in Windows, but you probably can!), and you need a more precise measurement:

    struct timeval start_tv, end_tv;
    struct timezone tz;

    gettimeofday(&start_tv, &tz);

    // code of what you want to measure

    gettimeofday(&end_tv, &tz);

    The idea is the same, but you can measure time in microseconds (1/1000000 of a second).
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Sorry, forgot to finish. To measure the difference you will require the following:

    struct timeval time_diff;
    unsigned long long_diff;

    time_diff.tv_sec = end_tv.tv_sec - start_tv.tv_sec;
    time_diff.tv_usec = end_tv.tv_usec - start_tv.tv_usec;

    long_diff = time_diff.tv_usec + (time_diff.tv_sec * 1000000);

    printf("Elapsed time is %5.06lf.", (double)(long_diff/1000000));
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  5. #5
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    RobS, that code doesn't seem to work.
    Ramble on...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    There isn't a good standard way of getting accurate times.

    Although struct timeval contains a value measured in microseconds, nothing states that the OS should update this value every microsecond. It's perfectly valid for the OS to update it once every 20mSec, by adding 20000 to it.

    For non-standard ways, depends on your OS and compiler (and processor).
    Win32 programmers should look at the getPerformanceCounter(sp?) function, which reads the very fast clock maintained by pentium processors.

    Or for DJGPP users, how about
    #include <stdio.h>
     * This macro found on
    #define RDTSC(llptr) ({ \
            __asm__ __volatile__ ( \
            ".byte 0x0f; .byte 0x31" \
            : "=A" (llptr) \
            : : "eax", "edx"); })
    int main ( ) {
        unsigned long long a, b;
        printf( "%lld -> %lld = %lld\n", a, b, b-a );
        return 0;

    > long_diff = time_diff.tv_usec + (time_diff.tv_sec * 1000000);
    this should be
    double diff = ( end_tv.tv_sec * 1000000.0 + end_tv.tv_usec ) -
                  ( start_tv.tv_sec * 1000000.0 + start_tv.tv_usec );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2001
    the clock() function is accurate isnt it?

    clock_t start, stop;

    start = clock();

    for(x=0; x<1000000; x++);

    stop = clock();

    printf("\nTime taken was %f seconds",(stop-start) / CLOCKS_PER_SEC);

    clock returns the number of clock 'ticks' since the program began and the clocks_per_sec macro converts it to seconds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM