Thread: Measure time

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Measure time

    When i am measuring the time of a bunch of statements in my C program using the following way, I am getting 0.00000 sec..

    Code:
    printf("\nTime taken : %fs",(double)t/CLOCKS_PER_SEC);
    Also am i getting the result in seconds? And how can I get the time in more high precision?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the result is seconds (assuming t is obtained from clock()). So if you know how many ticks it's for one second, you can calculate how many ticks it is for a millisecond and so on.

    printf("\nTime taken : %f",(double)t/(CLOCKS_PER_SEC/1000));

    For milliseconds. Just basic math.
    And %f is used to print floats and doubles. You don't need to do anything else (and shouldn't).
    Last edited by Elysia; 06-11-2008 at 01:11 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We can't answer that, we have no idea how you are obtaining the value in t.
    Show more code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Actually my program consists of few functions and I did like this..

    Code:
    int main()
    { 
        clock_t t; 
       // bunch of statments
    
        t = clock();
        
        //few functions
        
        t = clock() - t;
    
        printf("\nTime taken : %fs",(double)t*1000/CLOCKS_PER_SEC);
    
        return 0;
    I want to print the time at any precision but always time is being show 0.000000! Please help...Also my calculated time includes time taken to execute all the functions in the main..So is my code correct?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's because it doesn't take a second to execute all that! It probaly takes less than a millisecond!
    Again, don't use %fs - use ONLY %f.
    And divide t by the appropriate constant. One second = 1000 ms, 1 millisecond = 1000 microseconds (I think).
    So t = CLOCKS_PER_SEC / 1000 = milliseconds.
    t = CLOCKS_PER_SEC / 1000 / 1000 = microseconds
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    If you're on a UNIX system, you can use gethrtime(), which will get you a high-resolution timestamp. You make a call to gethrtime(), which returns a nuber of nanoseconds since some past event. Then execute the statement you want to time and call gethrtime() again. Subtract the two and you get the time taken in nanoseconds. run "man gethrtime" on UNIX, it even gives you an example of how to use it.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM