time function for linked list

This is a discussion on time function for linked list within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <time.h> #define LEN 50 typedef struct L { char k[LEN]; ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    time function for linked list

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <time.h>
    
    #define LEN 50
    typedef struct L { char k[LEN]; struct L *next; } List;
    
    List *insertList (char head[LEN], List *tail)
    {
      List *m = (List*) malloc (sizeof(List));
      strcpy(m -> k,head);
      m -> next = tail;
      return m;
    }
    
    void printList (List *v)
    {
    	while ( v != NULL){
    	printf ("%s\n", v -> k);
    	v = v -> next;
        }
    }
    
    static FILE *open_file ( char *file, char *mode )
    {
      FILE *fp = fopen ( file, mode );
    
      if ( fp == NULL ) {
        perror ( "Unable to open file" );
        exit ( EXIT_FAILURE );
      }
    
      return fp;
    }
    
    
    int main (int argc, char *argv[])
    {	
    	int x;
    	int wordcount;
    	char a;
    	char word[LEN];
    	List *t = NULL;
    	time_t start,end;
    	double dif;
    	FILE *fp;
    	time (&start);
    	fp = open_file ( argv[1], "r" );
    	wordcount = 0;
        do{
    	x = 0;
    	do{
    	a = fgetc (fp);
    	if (a != ' ' && a != '\n' && 
    		a != EOF && a !=',' && 
    		a !=';' && a !='.'&& 
    		a !='!' && a !='?' && 
    		a !=':' && a !='`'){
    	word[x] = tolower(a);
    	  x++;
    	  }else{
    	if (x != 0){
    		word[x] = '\0';
    	    t = insertList (word, t);
    		wordcount++;
    		}
    	}
          }while (a != ' ' && a != '\n' && 
    			  a != EOF && a !=',' && 
    			  a !=';' && a !='.'&& 
    			  a !='!' && a !='?' && 
    			  a !=':' && a !='`');      
        } while (a != EOF);
    	fclose (fp);
    	time (&end);
    	dif = difftime (end,start);
    	printList (t);
    	printf ("Total words read: %d\n", wordcount);
    	printf ("It took %.2lf seconds to read in words.\n", dif );
    
    	return 0;
    }
    time always returns 0.00 seconds. Where am i going wrong? thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i think this has been discussed on this board a few times.

    if you notice the man page of time and difftime that you looked at, there is an I/O opetartion between the two time calls. I/O, especially user input, takes a while, whereas simple for loops, arithmetic, boolean comparisons, dont take much time at all.

    i think the problem is that the precision of the time function is not nearly enough to measure the time it takes the cpu to do the operations you have being timed. even if you had a 1Mhz CPU, it can do up to 1,000,000 operations per second. of course you dont have a 1Mhz cpu, more likely a 1Ghz+, which is 1,000,000,000 operations per second.

    i think there are more precise timing functions but are probably non-portable. search this board or elsewhere for similar topics.

    edit: i was bored so i wrote this
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    
    int main (int argc, char *argv[])
    {	
    	int exponent = 1;
    	time_t start,end;
    	double dif;
    	double count;
    	
    	do
    	{
            printf("trying exponent &#37;d\n", exponent);
        	count = pow(2, exponent);    	
            time (&start);    	
            
        	while (count > 0)
    	      count--;
        
            time (&end);
    	  	dif = difftime (end,start);        
         } while (dif == 0 && exponent++);
    
    	printf ("you need at least 2^%d operations in order to slow down cpu to time it.\n", exponent );
    	printf("2^%d = %.0f operations took %.2fsec.\n", exponent, pow(2,exponent), dif);
    
        printf("press enter to exit\n");
        getchar();
    
    	return 0;
    }
    its the cheapest benchmark software available!
    Last edited by nadroj; 01-01-2008 at 12:54 PM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    yeah i was thinking the same thing about processors being too fast. guess i will have to look for other funcs other than difftime.cheers anyway

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    The clock isn't necessarily ticking during I/O operations anyway. If something is running slowly, use a profiler.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    With Citizen's comments in mind, clock() is a standard function that usually gives more precise timing information than time(). It, however, should shows the amount of CPU-time used by the process, rather than the amount of actual "wallclock" time, so something like this:
    Code:
    #include <time.h> 
    #include <stdio.h> 
    
    int main() 
    {
        clock_t t;
        t = clock();
        printf("Press enter...\n");
        getchar();
        t = clock() - t;
        printf("You took %d ticks to press enter\n", t);
        return 0;
    }
    may well end up with zero even if you wait for half a day before hitting enter. On other systems [particularly DOS-based systems].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    matsp, the code u suggested worked with my program. now the obvious question is whether there is a way of converting those ticks into seconds?

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    btw since time funcs results are platform dependent then i should mention that i am using mac os x 10.5 with xcode on it.

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by agentsmith
    time funcs results are platform dependent
    This isn't true. clock() and time() are standard C functions. In fact, all the time functions described here are part of standard C functions.

    Quote Originally Posted by agentsmith
    is whether there is a way of converting those ticks into seconds?
    I'll modify matsp code to show you how.

    Code:
    #include <time.h> 
    #include <stdio.h> 
    
    int main() 
    {
        clock_t t;
        t = clock();
        printf("Press enter...\n");
        getchar();
        t = clock() - t;
        printf("You took %f seconds to press enter\n", (double) t / CLOCKS_PER_SEC);
        return 0;
    }
    CLOCKS_PER_SEC is a macro defined in time.h.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The "platform dependancy" of clock() is that it will not ALWAYS return the CPU (processor) time used, but sometimes just the "walltime". It will however work as expected for most timing purposes where "most of the time is spent processing, and little time is used waiting for things". Of course, in DOS for example, even if you wait for a key-press, the CPU is probably sitting there spinning on your process, so it's actually correct to say that the CPU time spent was in the current executable - because there is no other process that could have used up the time...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    it all works now. time is returned in seconds just as i wanted. thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 11:42 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21