plz help.....time function

This is a discussion on plz help.....time function within the C++ Programming forums, part of the General Programming Boards category; I want to display the time spent during sorting the list....BUT i get 0 everytime...is it bcuz it doesnt take ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    plz help.....time function

    I want to display the time spent during sorting the list....BUT i get 0 everytime...is it bcuz it doesnt take less than a second to sort it..... here is the fucntion i am using:

    ----------------------------------------------
    float Time(int flag)
    {
    static clock_t start;
    clock_t end;
    if(flag==START) //START=0
    {
    start = clock();
    return 0.0;
    }
    else
    {
    end = clock();
    return (end-start)/CLK_TCK;
    }
    }
    --------------------------------------------------

    is there anyway to display the time even if its less than a second...

    plz help asap

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    double duration;
    duration = ((double)end - start) / CLK_TCK;
    printf("%lf", duration);
    If it still prints 0.000000, then try doing something that will take longer.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I assume that CLK_TCK is the same as CLOCKS_PER_SEC which is usually 1000 meaning that clock returns milliseconds.

    So if your program takes 1/10th of a second (100 milliseconds) then you are calculating 100/1000 with integers (no decimals or remainder) and the result is zero.

    If you're running windows... beware... Although clock() returns milliseconds, Windows does NOT update clock() every single millisecond. As I recall, it gets updated every 18 milliseconds. (This may have been corrected in WinXP... I'm outdated.)

    [EDIT]
    And, read the post above that says "Posting Code - Read This!" It explains about using code tags.
    Last edited by DougDbug; 04-30-2003 at 06:30 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    thanks guys ,it works now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21