Thread: Writing a function in C that times an operation

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Writing a function in C that times an operation

    So fat I have done this...


    Code:
    time_t  t0, t1;
    Code:
    t1 = time(NULL);
    printf ("Operation took %f seconds\n",(c1 - c0)/CLOCKS_PER_SEC);
    The problem is that more often that not, it will just return 0.00000, even though the operation has obviously taken much longer, am I doing something wrong?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need to use difftime instead of subtracting time_ts, because subtraction isn't guarenteed to work.
    Code:
    #include <stdio.h>
    #include <time.h>  /* required */
    
    int main(void) {
        time_t s = time(0);
    
        long_task();
    
        printf("That took %f seconds.\n", (double)difftime(s, time(0))/CLOCKS_PER_SEC);
    
        return 0;
    }
    The args to difftime might be reversed.
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Thanks for that, but its still outputting 0.000 seconds.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    ya that doesnt work for me, shouldn't use time (0), - i'm not getting zero but im getting a much smaller number than i should. the faq has a better solution which i've used myself before -

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    if you are getting 0.000, it might have something to do with not having any actual work done from when you start the timer to when you calculate the difference. the problem with the dwks solution is that time (0) is only changing every second... the faq uses processor ticks.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    difftime() returns a double, so there is no need for the cast
    difftime() returns the answer in seconds, so there is no need for the division.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    the problem with the dwks solution is that time (0) is only changing every second... the faq uses processor ticks.
    Well, not once a second, but it doesn't make for very accurate timing.
    Dev-Cpp/MS VS C++ 6.0, Windows XP
    I got a Win32 function that counts milliseconds to work under Dev-C++ . . . I don't remember the functions' names (there are two of them), but if you're interested I'll tell you next time I'm online.
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Strange, using the example, I ALWAYS get 2293.672000 seconds no matter what operation I do.

  9. #9
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    using the faq or dwks?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    The FAQ.

  11. #11
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    that is more than unusual, its standard code to the best of my knowledge. what compiler are you using, it would be very strange that its the cause of the problem...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    I'm using Dev-C++

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You'd better post your code then - you can stub out the work if you want, but at least show us how you're calculating time.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <time.h>
    
    
    typedef struct N {
        char      x[20];
        struct N *next;
    } List;
    
    List *insertlist(char word[15], List * b)
    {
        List *c = calloc(1, sizeof(List));
        strcpy( c->x, word ); 
        c->next = b;
        return c;
    }
    
    
    
    int search(char *query,char *query1, List * z)
    {
        int x=0;
     
      printf( "\nSearching through List for query...\n");
      while (z != NULL) {
        
        if ( strcmp(z->x,query) == 0 ) {
          x++;
        }
        z = z->next;
      }
      if(x==0) {
              printf("\nSorry! Your word was not found in the document\n");
              }
              else
              {
      printf("\nThe word %s was found %d times.\n", query1,x);
      return 0;
    }
    }
    
    lowercase(char *word) {
    
    int n = strlen(word) + 1;
    int i;
    for(i=0;i<n;i++){
    word[i] = tolower(word[i]);
    }  
    }
    
    int main(int argc, char *argv[])
    {
      
        clock_t start, end;
        long count;
        char word[15];
        char query[15];
        char query1[15];
        int   x;  
        int   y; 
        int   c;     
        List *z = NULL; 
        
        FILE *fp;
        
       
      
     
         
        fp = fopen(argv[1], "r");
      
        c = 0;
        while ( (x = fgetc(fp)) != EOF) {
            if (isspace(x) || ispunct(x)) {
                word[c] = '\0';
               
              
               
                c = 0;
                lowercase(word);
                z = insertlist(word, z);
            } else {
                word[c] = x;
              
                c = c + 1;
            }
            
        }
       
    
        
        
        end = clock();
        printf ( "The interval was: %f seconds\n",
        (double)( start - end ) / (double)CLOCKS_PER_SEC );
    
    
        fclose(fp);
      
        printf("\nPlease enter a search query:");
        scanf("%s",query);
        strcpy(query1,query);
        lowercase(query);
        search(query,query1, z);
         
        
    
    
            
        return 0;   
    }
    Last edited by Wiretron; 01-28-2006 at 01:00 PM.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So basically, you didn't initialise start at all, and you printed the answer BEFORE setting end?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM