Thread: Displaying Function Run Time

  1. #1
    Registered User
    Join Date
    Mar 2008

    Displaying Function Run Time

    Hi, I'm a beginner C programming student, and I need to display a function's run time on the display console.

    I know my time-calculating algorithm works (because if I change the location for the start-end times, I get a good, realistic number).

    However, for my function, I keep on getting a display time of 0. How can I extend my precision to show how long my function takes?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    The trouble is that standard C functions like time() and clock() do not have very good precision, as you have discovered. Sometimes 1/18.2 of a second accuracy; sometimes only 1 second accuracy.

    To get higher granularity, try Windows functions like QueryPerformaceCounter(). (I think that's the one.)

    [edit] Example:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <windows.h>
    LARGE_INTEGER stime;
    void start_time(void);
    long end_time(void);
    int main(int argc, char *argv[]) {
        char *call = 0, *p;
        size_t lcall = 0, len;
        int x;
        if(argc <= 1) {
            printf("\nusage: timeit command_to_time [args]\n");
            return 1;
        for(x = 1; x < argc; x ++) {
            len = strlen(argv[x]);
            p = realloc(call, lcall+len+1 + (x+1 < argc ? 1 : 0));
            if(!p) {
                fprintf(stderr, "Out of memory\n");
                return 1;
            call = p;
            strcpy(call+lcall, argv[x]);
            if(x+1 < argc) {
                call[lcall+len] = ' ';
                call[lcall+len+1] = 0;
                lcall ++;
            lcall += len;
        printf("&#37;li\n", end_time());
        return 0;
    void start_time(void) {
    long end_time(void) {
        LARGE_INTEGER diff;
        LARGE_INTEGER freq;
        diff.QuadPart -= stime.QuadPart;
        diff.QuadPart *= 1000;  /* Adjust to milliseconds, shouldn't overflow */
        return (long)(diff.QuadPart / freq.QuadPart);
    That's a Windows program I wrote a long time ago to time how long any arbitrary command takes to execute. It's not very well coded. (Note the missing <string.h>.) The times also do not take into account how slow system() is, but it's an okay example. Just ignore everything in main(). [/edit]
    Last edited by dwks; 03-21-2008 at 12:39 PM.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM