137 lines
7.6 KiB
HTML
137 lines
7.6 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<META name=vsisbn content="1576101746">
|
|
<META name=vstitle content="Michael Abrash's Graphics Programming Black Book, Special Edition">
|
|
<META name=vsauthor content="Michael Abrash">
|
|
<META name=vspublisher content="The Coriolis Group">
|
|
<META name=vspubdate content="07/01/97">
|
|
<META name=vscategory content="Web and Software Development: Game Development,Web and Software Development: Graphics and Multimedia Development">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<TITLE>Michael Abrash's Graphics Programming Black Book Special Edition: Assume Nothing</TITLE>
|
|
|
|
<!-- HEADER -->
|
|
<!-- Empty Reference Subhead -->
|
|
|
|
<!--ISBN=1576101746//-->
|
|
<!--TITLE=Michael Abrash's Graphics Programming Black Book Special Edition//-->
|
|
<!--AUTHOR=Michael Abrash//-->
|
|
<!--PUBLISHER=The Coriolis Group, Inc.//-->
|
|
<!--CHAPTER=03//-->
|
|
<!--PAGES=065-067//-->
|
|
<!--UNASSIGNED1//-->
|
|
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
|
|
|
|
<CENTER>
|
|
<TABLE BORDER>
|
|
<TR>
|
|
<TD><A HREF="03-07.html">Previous</A></TD>
|
|
<TD><A HREF="index.html">Table of Contents</A></TD>
|
|
<TD><A HREF="03-09.html">Next</A></TD>
|
|
</TR>
|
|
</TABLE>
|
|
</CENTER>
|
|
<P><BR></P>
|
|
<P>Moreover, because it uses an undocumented feature, the timer-stop approach could conceivably cause erratic 8253 operation, which could in turn seriously affect your computer’s operation until the next reboot. In non-8253-compatible systems, I’ve observed not only wildly incorrect timing results, but also failure of a diskette drive to operate properly after the long-period Zen timer with <B>PS2</B> set to 0 has run, so be alert for signs of trouble if you do set <B>PS2</B> to 0.</P>
|
|
<P>Rebooting should clear up any timer-related problems of the sort described above. (This gives us another reason to reboot at the end of each code-timing session.) You should <I>immediately</I> reboot and set the <B>PS2</B> equate to 1 if you get erratic or obviously incorrect results with the long-period Zen timer when <B>PS2</B> is set to 0. If you want to set <B>PS2</B> to 0, it would be a good idea to time a few of the listings in this book with <B>PS2</B> set first to 1 and then to 0, to make sure that the results match. If they’re consistently different, you should set <B>PS2</B> to 1.</P>
|
|
<P>While the the non-PS/2 version is more dangerous than the PS/2 version, it also produces more accurate results when it does work. If you have a non-PS/2 PC-compatible computer, the choice between the two timing approaches is yours.</P>
|
|
<P>If you do leave the <B>PS2</B> equate at 1 in Listing 3.5, you should repeat each code-timing run several times before relying on the results to be accurate to more than 54 ms, since variations may result from the possible lack of synchronization between the timer 0 count and the BIOS time-of-day count. In fact, it’s a good idea to time code more than once no matter which version of the long-period Zen timer you’re using, since interrupts, which must be enabled in order for the long-period timer to work properly, may occur at any time and can alter execution time substantially.</P>
|
|
<P>Finally, please note that the <I>precision</I> Zen timer works perfectly well on both PS/2 and non-PS/2 computers. The PS/2 and 8253 considerations we’ve just discussed apply <I>only</I> to the longZen timer.</P>
|
|
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Example Use of the Long-Period Zen Timer</FONT></H3>
|
|
<P>The long-period Zen timer has exactly the same calling interface as the precision Zen timer, and can be used in place of the precision Zen timer simply by linking it to the code to be timed in place of linking the precision timer code. Whenever the precision Zen timer informs you that the code being timed takes too long for the precision timer to handle, all you have to do is link in the long-period timer instead.
|
|
</P>
|
|
<P>Listing 3.6 shows a test-bed program for the long-period Zen timer. While this program is similar to Listing 3.2, it’s worth noting that Listing 3.6 waits for a few seconds before calling <B>ZTimerOn</B>, thereby allowing any pending keyboard interrupts to be processed. Since interrupts must be left on in order to time periods longer than 54 ms, the interrupts generated by keystrokes (including the upstroke of the Enter key press that starts the program)—or any other interrupts, for that matter—could incorrectly inflate the time recorded by the long-period Zen timer. In light of this, resist the temptation to type ahead, move the mouse, or the like while the long-period Zen timer is timing.</P>
|
|
<P><B>LISTING 3.6 LZTEST.ASM</B></P>
|
|
<!-- CODE //-->
|
|
<PRE>
|
|
; Program to measure performance of code that takes longer than
|
|
; 54 ms to execute. (LZTEST.ASM)
|
|
;
|
|
; Link with LZTIMER.ASM (Listing 3.5). LZTIME.BAT (Listing 3.7)
|
|
; can be used to assemble and link both files. Code to be
|
|
; measured must be in the file TESTCODE; Listing 3.8 shows
|
|
; a sample file (LST3-8.ASM) which should be named TESTCODE.
|
|
;
|
|
; By Michael Abrash
|
|
;
|
|
mystack segment para stack ‘STACK’
|
|
db 512 dup(?)
|
|
mystack ends
|
|
;
|
|
Code segment para public ‘CODE’
|
|
assume cs:Code, ds:Code
|
|
extrn ZTimerOn:near, ZTimerOff:near, ZTimerReport:near
|
|
Startproc near
|
|
push cs
|
|
pop ds ;point DS to the code segment,
|
|
; so data as well as code can easily
|
|
; be included in TESTCODE
|
|
;
|
|
; Delay for 6-7 seconds, to let the Enter keystroke that started the
|
|
; program come back up.
|
|
;
|
|
mov ah,2ch
|
|
int 21h ;get the current time
|
|
mov bh,dh ;set the current time aside
|
|
DelayLoop:
|
|
mov ah,2ch
|
|
push bx ;preserve start time
|
|
int 21h ;get time
|
|
pop bx ;retrieve start time
|
|
cmp dh,bh ;is the new seconds count less than
|
|
; the start seconds count?
|
|
jnb CheckDelayTime ;no
|
|
add dh,60 ;yes, a minute must have turned over,
|
|
; so add one minute
|
|
CheckDelayTime:
|
|
sub dh,bh ;get time that’s passed
|
|
cmp dh,7 ;has it been more than 6 seconds yet?
|
|
jb DelayLoop ;not yet
|
|
;
|
|
include TESTCODE ;code to be measured, including calls
|
|
; to ZTimerOn and ZTimerOff
|
|
;
|
|
; Display the results.
|
|
;
|
|
call ZTimerReport
|
|
;
|
|
; Terminate the program.
|
|
;
|
|
mov ah,4ch
|
|
int 21h
|
|
Start endp
|
|
Code ends
|
|
end Start
|
|
</PRE>
|
|
<!-- END CODE //-->
|
|
<P>As with the precision Zen timer, the program in Listing 3.6 is used by naming the file containing the code to be timed TESTCODE, then assembling both Listing 3.6 and Listing 3.5 with MASM or TASM and linking the two files together by way of the Microsoft or Borland linker. Listing 3.7 shows a batch file, named LZTIME.BAT, which does all of the above, generating and running the executable file LZTEST.EXE. LZTIME.BAT assumes that the file LZTIMER.ASM contains Listing 3.5 and the file LZTEST.ASM contains Listing 3.6.
|
|
</P><P><BR></P>
|
|
<CENTER>
|
|
<TABLE BORDER>
|
|
<TR>
|
|
<TD><A HREF="03-07.html">Previous</A></TD>
|
|
<TD><A HREF="index.html">Table of Contents</A></TD>
|
|
<TD><A HREF="03-09.html">Next</A></TD>
|
|
</TR>
|
|
</TABLE>
|
|
</CENTER>
|
|
|
|
<hr width="90%" size="1" noshade>
|
|
<div align="center">
|
|
<font face="Verdana,sans-serif" size="1">Graphics Programming Black Book © 2001 Michael Abrash</font>
|
|
</div>
|
|
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
|
|
<!-- reference_subfoot = footer -->
|
|
<!-- reference_footer = subfoot -->
|
|
|
|
<!-- BEGIN SUB FOOTER -->
|
|
</BODY>
|
|
</HTML>
|
|
|
|
<!-- END FOOTER -->
|
|
|
|
|