Half Second

This is a discussion on Half Second within the C++ Programming forums, part of the General Programming Boards category; I know I can use the time(NULL) function to measure amounts of time as little as one second, but how ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    28

    Half Second

    I know I can use the time(NULL) function to measure amounts of time as little as one second, but how can I measure less than one second?
    ---Rainer
    Digital pimp, hard at work

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Milliseconds

    C++ can normally measure time down to the millisecond (1/1000 th of a second)... 1/2 second = 500ms.

    There is a constant defined in time.h called CLOCKS_PER_SEC. This seems to always be 1000, but I get the feeling this is only a psuedo-standard (?)

    The function clock() will return the number of counts (i.e. milliseconds).

    On Windows machines, clock() will return the number of milliseconds, but it only gets updated about every 50 milliseconds... I've forgotten exactly.

    See the Programming FAQ

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Yeah, CLOCKS_PER_SEC is standard though it's value isn't. Values between 1 and 1000 seem usual though.

    Also, clock() measures program time, not real time. If your program spends a lot of time sleeping or waiting for things to happen, then clock() will lag.

    Basically, anything sub-second is specific to your OS/Compiler. So say what you have if you want a more specific answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    28
    I want to be able to run my program in any win32 environment (since it's ANSI, it shouldn't matter, right?) and I have the worst compiler out there, Dev version 4. I've been meaning to get a better one...
    ---Rainer
    Digital pimp, hard at work

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    If you need about a half-second you should be OK most of the time...

    However, you simply cannot count on accurate timing (in user mode) with a multi-tasking operating system. In user mode, another program can interrupt your program at any time... maybe in the middle of your half-second count... When it returns to your program, it may be two seconds later. To get total control of the system, and lock-out other concurrent processes and interrupts, you have to get into the kernel mode. (Not easy stuff... and I don't know how to do it.)

    There is a Windows function, I think it's GetTickCount(), that will return milliseconds accurately (at the time it's executed). But, of course that function will only execute during your program's time-slice.
    Last edited by DougDbug; 11-18-2003 at 03:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (newb) Summing half of an array
    By quiet_forever in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2007, 10:45 AM
  2. Half Humanoid, half Chimpanzoid
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-19-2004, 04:23 PM
  3. half an int
    By ninebit in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2002, 01:24 AM
  4. Half life Problem which I am right and the teacher is wrong
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 11-06-2002, 03:28 PM
  5. The glass is half full
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-30-2002, 02:08 PM

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