Evaluate time in C++

This is a discussion on Evaluate time in C++ within the C++ Programming forums, part of the General Programming Boards category; anyone can help me?? How to evaluate time between 2 event!!...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Viet nam
    Posts
    9

    Evaluate time in C++

    anyone can help me??
    How to evaluate time between 2 event!!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    var1 = clock();
    // Do something
    var2 = clock();
    diff = var2 - var1;

    Perhaps?
    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
    Registered User
    Join Date
    Apr 2008
    Location
    Viet nam
    Posts
    9
    thanks !! I'll try!!!
    ^.^
    P/s can you tell me the types of var1 and var 2?? (int/ double/ or others??)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    The return type of clock (according to MSDN, clock_t).
    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.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Viet nam
    Posts
    9
    Can you explain clearly!! T.T I don't understand!! (
    And time in here count by second or other??
    Last edited by nghoanglinh223; 04-02-2008 at 08:05 AM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    What do you not understand?
    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.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Location
    Viet nam
    Posts
    9
    Can you explain if I use types clock_t for var1 and var2 .... they'll counted by second or other,wont' they??
    And what's the difference between clock_t and MSDN??
    Thanks you!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nghoanglinh223 View Post
    Can you explain clearly!! T.T I don't understand!! (
    And time in here count by second or other??
    The time is not seconds, but "ticks", and there is #define constant in clock.h called CLOCKS_PER_SEC, which you can divide the result by to give you seconds. If you are measuring relatively short times, you should probably also convert your time to float or double.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by nghoanglinh223 View Post
    Can you explain if I use types clock_t for var1 and var2 .... they'll counted by second or other,wont' they??
    The function returns some sort of "ticks," that is defined by a special define somewhere.
    clock_t is a type which clock() returns, simply put. You should use that type to ensure the code works.

    And what's the difference between clock_t and MSDN??
    clock_t is a type and MSDN is a place on the web for documentation about programming.
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nghoanglinh223 View Post
    And what's the difference between clock_t and MSDN??
    Thanks you!
    MSDN is "MicoSoft Developers Network", and "clock_t" is the type that clock() returns as per the ANSI C89/ISO C90 standard (and earlier pseudo-standards).

    So to compare those is like comparing an apple to a car - they don't compare very well, because they have completely different purpose and meaning.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Location
    Viet nam
    Posts
    9
    Quote Originally Posted by matsp View Post
    The time is not seconds, but "ticks", and there is #define constant in clock.h called CLOCKS_PER_SEC, which you can divide the result by to give you seconds. If you are measuring relatively short times, you should probably also convert your time to float or double.

    --
    Mats
    Can you give me a example,plz!! ^.^
    I have tried in this code:

    int var1,var2,diff;

    var1 = CLOCKS_PER_SEC;

    ab.saveToFile("ATM.txt",key);

    var2= CLOCKS_PER_SEC;

    cout << " :" << var2;

    diff = var2-var1;

    cout << endl<<diff <<endl;


    and the result is var1 = 1000;
    var 2 =1000
    diff = 0 :|

    And i can't include clock.h

    Thanks!!

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Hello? You are supposed to call clock(). And we already told you the type is clock_t.

    Code:
    clock_t var1,var2,diff;
    var1 = clock();
    ab.saveToFile("ATM.txt",key);
    var2 = clock();
    cout << " :" << var2;
    diff = var2-var1;
    cout << endl<<diff / CLOCKS_PER_SEC <<endl;
    It's supposedly residing in time.h.
    The documentation also explicitly states
    (elapsed time in seconds times CLOCKS_PER_SEC)
    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.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you should use var1= clock() and var2 = clock(), then DIVIDE the resulting difference by CLOCKS_PER_SEC().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Location
    Viet nam
    Posts
    9
    Thanks you so much!!

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    I would skip clock() and go for gettimeofday() if I were you. clock() is kind of not portable. It is defined everywhere (as far as I can tell), but have different meanings. IIRC on GCC/Windows, it measures the wall time (real time), on GCC/Linux, it measures CPU time (sometimes significantly less than real time).

    Or, if you only need second precision, there is always time().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  4. Checking parts of a structure
    By DocDroopy in forum C Programming
    Replies: 11
    Last Post: 08-05-2002, 07:45 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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