Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dannyxyz23

Timing in C

This topic is 5768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I get an accurate function to get time in C? I''ve tried clock, but I need more precision. I just want to count the time between two events, but in our "good and old" console mode. Thank you all!

Share this post


Link to post
Share on other sites
Advertisement
The standard C library only offers access to the RTC (real-time clock) which ''ticks'' 18.6 times per second. If you want better precsision, you need to use OS-dependent functions (e.g. Windows: timeGetTime() (millisecond), QueryPerformanceCounter() (CPU-dependent), Unix: gettimeofday() (microsecond) )


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Thanks.
But what library do I need to include to get those timeGetTime() functions to work on Dos? Do I have to program my game under Windows to get access to them?
Is there any assebly in line coding which I could use?
Thanks again!

Share this post


Link to post
Share on other sites
High resolution timers in DOS:

TSC
+ resolution of 1 / CPU freq
+ quick to read
- you''re hosed on SMP / laptop systems
- Pentium+ required

__asm
{
cpuid
rdtsc
; TSC in edx:eax
}


8254 PIT
+ available on every pc
+ fixed, pretty high resolution (0.84 µs)
- slow to read (2x in => several µs)

__asm
{
in al, 0x40
mov dl, al ; grr - why lo byte first? :-|
in al, 0x40
mov dh, al
; ticks in dx
}

NOTE: you''re toast if you just caught the PIT at the end of a cycle. Better latch the count reg first (see 8254 dox).

If you''re willing to poll, you can also check the RAM refresh signal from PIT channel 1 (read port 0x61, bit 4, which flips every 18 PIT clocks).

Fruny: the RTC counts seconds, and can be used to generate a periodic interrupt (32 KHz input clock, programmable rate of 2..8192 ints / sec). The PIT is usually set to square wave operation with a freq of 1.193 MHz (input; = PC sys clock / 12) / 65536 (BIOS default, also programmable) = 18.2 Hz.


If you''re running under Windows, you can''t reliably read from the PIT anymore, but you can use QueryPerformanceCounter (which either uses the PIT internally, or the mobo PCI timer, so it''s not any faster).

HTH
Jan

Share this post


Link to post
Share on other sites
Ok, its inline assembly, but how do I know what I am doing there?
I mean, where is the time being caught and how do I compile that under DJGPP?

Share this post


Link to post
Share on other sites
> how do I know what I am doing there?
Why, you could start by RTFM
Link
Link
Link

> how do I compile that under DJGPP?
I''m told that GAS now supports intel syntax. Dunno whether your DJGPP has the newer GAS; if not, you get to write awful AT&T syntax
Read Link

500x1

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!