Thread: Working with clock(); getting unexpected result

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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

  8. #8
    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.

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