Thread: Working with clock(); getting unexpected result

  1. #1
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67

    Working with clock(); getting unexpected result

    Hello everyone,

    I have written a program to answer Problem 54 on Project Euler and I have the code working successfully, but the problem I'm having is related to measuring how long it takes the program to run.

    I am working on Fedora 12 running on a Virtual PC. The result I am currently getting is:

    Code:
    time lapsed: 0.000000
    I am expecting a different result as it has to take 'some' time for it to process. Any help would be appreciated. Below is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    //defines listed here
    //function prototypes listed here
    
    int main(void) {
    	char hand1[11] = {0}, hand2[11] = {0};
    	char *filename = "poker.txt";
    	FILE *fin;
    	int ch = 0, counter = 0, i = 0, j = 0;
    	static int p1_wins = 0, p2_wins = 0;
    	clock_t start, end;
    	double lapsed = 0.0;	
    
    	fin = fopen(filename, "r");
    	if (fin == NULL) {
    		fprintf(stderr, "Error: unable to open %s for reading.\n", filename);
    		exit (1);
    	}	
    
    	start = clock();
    
    	while ((ch = fgetc(fin)) != EOF) {
    		if ((ch == ' ') || (ch == '\r'))
    			continue;
    
    		if (counter < 10) {
    			hand1[i] = ch;	
    			i++;	
    			counter++;
    		}		
    
    		else if (counter >= 10) {
    			if (ch == '\n') {
    				counter = 0;
    				i = 0;
    				j = 0;
    				determine_winner(hand1, hand2, &p1_wins, &p2_wins);
    			}
    
    			else {
    				hand2[j] = ch;
    				j++;
    			}
    		}
    	}	
    
    	printf("p1_wins: %d\n", p1_wins);
    	printf("p2_wins: %d\n", p2_wins);	
    
    	end = clock();
    
    	lapsed = (double)(end - start)/CLOCKS_PER_SEC;	
    
    	printf("time lapsed: %f\n", lapsed);
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Probably it's not long enough?
    How large is your file?
    Last edited by Bayint Naung; 06-02-2010 at 06:48 AM.

  3. #3
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    The code is 400+ lines. But I'm not sure that's the problem. From my understanding of clock(), it returns the processor cpu time. The cpu has to do some work, so I'd expect to get something other than 0.000000 unless I have implemented the function incorrectly.

    Alternatively, I've checked the return value of clock() both for start and end, to see if I am getting a -1 (value unavailable), and I am not

    Example:

    Code:
    start = clock();
    if (start == -1)
    	printf("start time unavailable\n");
    EDIT:

    I've printed out the value of CLOCKS_PER_SEC on my system and it comes to 1000000, therefore if the difference of (end - start) were only 5, I should be getting 0.000005 as an answer
    Last edited by matrixx333; 06-02-2010 at 07:13 AM. Reason: more info

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I think your program will be IO bound.
    Try to change fin to no buffer mode.
    Code:
    setvbuf(fin,NULL,_IONBF,0);
    See any difference?

  5. #5
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    TY for the help Bayint Naung, but unfortunately when using setvbuf() I am still getting the same result. I placed the function after the stream has been opened but before any operation has been done on the stream.

    I created a small program just to test if my computer was the issue, and it is not. I got 0.217000 when running the code below:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void) {
    	int i = 0;
    	clock_t start, end;
    	double lapsed = 0.0;
    	
    	start = clock();
    	
    	for (i=0; i<10000; i++)
    		printf("i: %d\n", i);
    		
    	end = clock();
    	
    	lapsed = (double)(end - start)/CLOCKS_PER_SEC;
    	
    	printf("lapsed: %f\n", lapsed);
    	
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    How large is your input poker.txt file ?
    Is determine_winner() function doing anything that could take time??

  7. #7
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Here are both of the files:

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Matrix,
    Try putting your little timewaster loop into your program just before the "end" statement.
    You know it takes measurable time, so if your program still reports 0, there is a problem.
    Otherwise, your code is too fast. (I never thought I'd write that sentence.)
    Gil

  9. #9
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    TY for the reply gil,

    There must be a problem. I did as you suggested and I'm still arriving at 0.000000. I even changed my printf() statement to show twenty 0's after the decimal point:

    Code:
    printf("time lapsed: %.20f\n", lapsed);
    and I'm still arriving at the same answer. Unfortunately, I don't know that much about the proper way to use clock(), hence my original question. I hope someone will be able to shed some light on this interesting issue


  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you set your print statement to output beyond the range of the data type, you're just telling the printf() to print nonsense digits -- no relation to anything factual.

    Clock() comes from the PIC chip, and only gets 1/3rd of that, so it's resolution is limited. Plus, your OS is multi-tasking (unlike DOS or a real-time OS), and so each request is put into a prioritized list.

    Your motherboard and Linux may have higher resolution timer chips and code for them, so check them out. There are at least two different types - one was a "multimedia timer" solution and another one involved a chip from Intel. The latter is a good one. The "multimedia timer" was a mess.

    Google "high precision Intel timer" and see what you can find. I haven't seen the code and/or driver needed, for this, yet.

  11. #11
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    WOW! Excellent call Adak!

    I took your advise and before doing in-depth research on my mobo and processor I copied the program over to my Intel Atom Netbook......and the function worked perfectly! I got an answer of 0.031000.

    The machine I was originally running the program from has a AMD Athlon 64 X2 6000+ Windsor 3.0GHz with a GIGABYTE GA-M61P-S3 mobo....

    I'll start googling HPET with my hardware specs to see if I can find any info on getting this working on a AMD machine....thanks again Adak!

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    matrix,
    Your main program doesn't work because of an extra pair of parentheses around
    (end - start)/CLOCKS_etc.

    (double)(end - start)/CLOCKS_etc.

    works to give a floating point number

    (double)((end - start)/CLOCKS_etc.)

    gives you an integer result which is then converted to double.

    Added later - When I copied your code to a text editor, I saw the extra parentheses, which I now see don't show in your post.
    Try copying the line from your test program to the main program.
    Sorry about the confusion,
    Gil
    Last edited by gil_johnson; 06-03-2010 at 05:33 AM.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Rats - I see your code doesn't show the parentheses, but I saw them when I copied the code to a text editor.
    You might try copying the line from your test program over the line in the main program.
    Gil

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    nice catch, gil_johnson.
    Why don't you post the exact code that caused problem? matrix333???!!!!!!!

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    matrix,
    I'm sorry - I am away from home, where I worked on your program, and my mouse here has died.
    I am getting around by keyboard, but I can't figure out how to select the text in your code section.
    What I did was to highlight the code in your original post by clicking and dragging over the whole program, hitting Ctl-C, and pasting into mousepad, a text editor.
    I'll post again later today if you can't reproduce the result I saw.
    Gil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with structures and classes
    By jdcollins in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2009, 05:07 PM
  2. endian conversion...
    By roaan in forum C Programming
    Replies: 12
    Last Post: 08-10-2009, 05:54 PM
  3. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  4. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM