How to simulate time in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • curiously enough
    New Member
    • Aug 2008
    • 79

    How to simulate time in C++

    How do I simulate time in C++?
    For example I want:
    Code:
    printf("\a\a\a");
    half a second passes;
    printf("\a\a");
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I'd recommend the Sleep() function.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      It depends on what libraries you've got. The sleep function is not part of the language specification, so it isn't present on all platforms.

      I'm not familiar with the C++ Standard Library, but in C the most portable method is to spin on the time() function until difftime() tells you that the desired amount of time has passed. Refer to <time.h>.

      A spin-loop like this is just about the worst thing you can do in a multi-tasking environment. You may have to use a nonportable technique. If so, then we would need to know more about your run-time environment -- especially what operating system you're using.

      Comment

      • Arepi
        New Member
        • Sep 2008
        • 62

        #4
        Hi!

        You can use also the function delay(millisec) ; (#include <dos.h>)
        when it possible in your system

        Comment

        • Studlyami
          Recognized Expert Contributor
          • Sep 2007
          • 464

          #5
          Well this will depend on how precise you need the timer. On a windows platform you can use GetTickCount() if you only need the time in milliseconds and you don't need a long lasting timer. You can also create a timer, but its best if you just read up on them.. If you need a more percise timer you can goolge for high resolution timers in C++ and come up with a lot of information about them.

          Comment

          Working...