Thread: How to time how long a program takes to run?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    15

    How to time how long a program takes to run?

    Hi

    I have written a C program that is supposed to run in under 2 seconds. Is there a function in C that lets me time how long it takes to run the program?

    Thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I often used clock() in time.h to get the time in millisecs. Only problem with that is that returns the number of clock cycles on linux so you have to divide it by CLOCKS_PER_SEC according to the manpage. Anyway its simple to use:
    Code:
    start_time = clock()
    // do stuff
    end_time = clock()
    seconds_elapsed = (end_time - start_time) / 1000;
    Alternatively theres gettimeofday(), which is more precise.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    On Linux I'd be using time anyway.

    Code:
    [ ~]$ time ./myprog
    
    real	0m3.855s
    user	0m0.057s
    sys	0m0.297s
    Of course that method also counts how long your program takes to start.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. can't get this program to run correctly
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2008, 04:16 PM
  3. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  4. Replies: 3
    Last Post: 03-21-2004, 06:02 PM
  5. Replies: 5
    Last Post: 04-17-2003, 07:07 AM