Thread: How to time something in C?

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    How to time something in C?

    I have to find the time that my program does to create a BST from a file given.First i tried to have a counter,but then i realised that this counter counts how many lines of the file i read.Is there anything-some functions maybe-which can do the job?
    Code:
    while ( fgets(line, LINE_LENGTH, fp) != NULL)
    {
    	count_lines++;
            Tree_insert(BulkTree , line , &error);
            if(error)
            {
    			printf("Sorry,can not allocate memory . Read %u 
                            lines",count_lines);
    			return -1;
    		}
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you time anything with a clock? See what time it is when you start. See what time it is when you end. Do some math.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok.I searched the net and found function time.However i can not write the right implemention.
    Code:
    int main(void)
    {
    	time_t now1,now2,now;
    	int n;
    	now1=time(NULL);
    	n=getchar();
    	now2=time(&now);
    	now=now2-now1;
    	printf("%d\n",now);/* I do not know what the letter after % should be so i put d */
    }
    I run this small program but the output is always 0.What is wrong?:/

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to wait at least one second before pressing enter.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    15
    You're passing NULL to the first call to time (edit: probably fine anyway). Also, don't bother doing the subtraction yourself, you can call difftime() to do this for you.

    Code:
    time_t pre_time, post_time;
    double elapsed;
    
    pre_time = time(&pre_time);
    
    .. do stuff
    
    post_time = time(&post_time);
    elapsed = difftime(post_time, pre_time);
    Use a std library reference to see how you use the various functions, such as this one:
    The C Standard Library
    Last edited by deepcode; 05-14-2011 at 05:02 PM.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    You need to wait at least one second before pressing enter.
    I did,but the program is not ok i think.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I counted four seconds before hitting enter, and it gave me 4. I counted ten seconds before hitting enter, and it told me 9 (probably because I was too fast with the counting). Don't know what more you want.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by deepcode View Post
    You're passing NULL to the first call to time (edit: probably fine anyway). Also, don't bother doing the subtraction yourself, you can call difftime() to do this for you.

    Code:
    time_t pre_time, post_time;
    double elapsed;
    
    pre_time = time(&pre_time);
    
    .. do stuff
    
    post_time = time(&post_time);
    elapsed = difftime(post_time, diff_time);
    Use a std library reference to see how you use the various functions, such as this one:
    The C Standard Library
    Thank you very much.This works.Second argument in difftime is pre_time and not diff_time .I was looking in libraries but i looked for time and localtime.You see this is my firts year in DIT ,athens and so i am still learnig.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    I counted four seconds before hitting enter, and it gave me 4. I counted ten seconds before hitting enter, and it told me 9 (probably because I was too fast with the counting). Don't know what more you want.
    I run it again now and yes it worked.Sorry:/ . So you are right too.Thanks a lot

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    15
    Yep, typo, fixed it

    I just figured this one out myself about a month ago. Knew what I had to do, just had to find the right functions for doing it.
    Always going back and forth checking params and reading whats available in the std lib whenever learning something new.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by std10093 View Post
    Ok.I searched the net and found function time
    Do you not have the library documentation for your compiler?
    I'm pretty sure most of them come with it as part of the install.

  12. #12
    Registered User Alexander.'s Avatar
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    I guess using time() does not provide a very accurate granularity. (you said timing was 0 which could really mean 0.5 seconds). Most platforms provide a simple near millisecond granularity if you require this through various system APIs or interrupts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 05-22-2011, 11:20 PM
  2. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  3. time program showing time since epoch?
    By cus in forum Linux Programming
    Replies: 5
    Last Post: 01-10-2009, 01:56 PM
  4. Replies: 3
    Last Post: 06-13-2003, 06:47 AM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM