Milliseconds

This is a discussion on Milliseconds within the C++ Programming forums, part of the General Programming Boards category; I need help on counting milliseconds. I tried using a pointer to the system clock (I think), but it only ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Milliseconds

    I need help on counting milliseconds. I tried using a pointer to the system clock (I think), but it only counts ticks (1/18.2 seconds). I need real milliseconds.
    And not a delay function like sleep().
    It should work something like this:

    int main()
    {
    StartCount();
    DoSomething();
    int MilliSec=StopCount();
    //Return number of milliseconds passed
    return 0;
    }

    Someone who can help?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I think this is another of these "depends on the compiler and OS" type answers.

    With Win32 you can call GetLocalTime(), (or GetSystemTime() of course), in your StartCount, then again in your StopCount, and subtract the earlier time from the later time to give the elapsed time in millisecond.

    The details of how you do this on other platforms I don't know, but the concept remains the same.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Ooops!!!

    Ooops!!!
    Forgot to mention. I'm using Borland 5.0 (DOS environment, not windows)

    Is it possible (probably not recomended ) to use GetSystemTime() within DOS?
    Perhaps someone else know another way?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Nope. It can be done under windows, but under DOS, you'd probably have to write it in ASM.

    I'll check one of my ASM references, maybe I can cook something up.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Here's a program which does just this -- reporograms the timer to give millisecond accuracy.

    Note!! This probably will only work in real, true, authentic DOS. Windows surely reprograms the timers, so it might barf on you if you mess with them.

    http://www.totse.com/en/computers/c_.../millisec.html

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Hmm

    > These routines reprogram the timer chip

    Sounds dangerous... Anyway, I run this program from windows so I should probably not use this method .

    While speaking of DOS, what is the exact difference between DOS and console DOS? Is any DOS program run from windows a console DOS program? Or is it only those who "looks like" a console window? (like the "Hello World" programs)

    I have been playing around with mode13 graphics for a while now, does that count as "real DOS" or console DOS?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    All of this depends on the OS.

    If you are in windows 95, 98, Me, you have 3 flavors:

    1) Pure DOS -- you must shut down to a DOS prompt. This *is* DOS. It uses segment:offset addressing, DOS executables, and all DOS/BIOS functions are permitted. WinAPI obviously can't be used.

    2) DOS under Windows -- executes in a command prompt, permits a lot of DOS commands not supported under console. Uses segment-offset addressing, uses DOS EXE file format, cannot use WinAPI commands, cannot use DOS/BIOS functions that will cause problems with Windows.

    3) Console -- this is a windows program fully. It uses the windows PE file format, cannot run under pure DOS, can use all WinAPI commands, uses flat addressing, etc.

    Note that, programmatically, 1 and 2 are the same -- they are DOS programs. The restrictions on 2) are imposed because it's executing under another operating system.

    Under NT series operating systems (NT 4, 2000, XP) you only have a DOS emulator, so you can't use pure DOS. DOS under Windows is more restrictive than in 95/98 because of the emulator. The console is the same (although there are security considerations in NT4 or 2000 that you must worry about).

    If you are using Mode13, you'd fall under type 2) -- it's truly a DOS program, being executed under Windows. Console programs are only allowed text modes, not graphical.
    Last edited by The V.; 11-01-2001 at 01:15 PM.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Thanks

    Thanks for the explanation (about DOS).

    About the millisec-code you linked, will it work in Mode13 or will it cause problems with windows?

    Anyway, thanks for your help
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    It would work IF you shut down to pure DOS (on a 95/98/Me machine).

    It probably won't work if you run it under windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time in milliseconds
    By coder_009 in forum C Programming
    Replies: 4
    Last Post: 05-09-2008, 03:36 AM
  2. time.h ctime and milliseconds
    By Deewhyandy1 in forum C Programming
    Replies: 4
    Last Post: 01-26-2008, 12:42 AM
  3. text comparing
    By Picachu in forum C Programming
    Replies: 48
    Last Post: 12-14-2006, 02:44 AM
  4. Current System Time (In Milliseconds)
    By IGAU in forum C Programming
    Replies: 10
    Last Post: 03-30-2004, 05:53 PM
  5. Time in milliseconds?
    By d00b in forum C++ Programming
    Replies: 3
    Last Post: 08-05-2002, 09:33 PM

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