TGC Codebase Backup



Program Cycles in Hertz by Galiem Vaelant

29th Dec 2004 13:59
Summary

This short bit of code will calculate and return the number of cycles per second your program runs at in hertz, the SI unit for cycles per second.



Description

This short bit of code will calculate and return the number of cycles per second your program runs at in hertz, the SI unit for cycles per second.

This code will produce that value in a formatted string: PC@__Hz, where __ is the number of cycls per second. Displaying this string in your program can be useful for finding spots where your program slows down, stops, etc. Thus, this can be a great tool for performance tweaking.

The average that I've seen so far for a user interface that works exactly like the Windows API is 39 to 42 Hz.

Using the equations in the note below, this could be expanded to produce a full wave function representing your program's performance. If nobody else does so, I may add that at a later date.

Note: Hz = Hertz = Cycles/ Second = Frequency/ Wavelength



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `Ensure that the below is processed before your program enters its main loop
DIM loops(1) : loops(0) = 0
DIM start_time(1) : start_time(0) = TIMER()
DIM hertz(1) : hertz(0) = 0

`Below is your main loop
DO
INC loops(0)

IF TIMER() - start_time(0) >= 1000
time_thus_far = TIMER() - start_time(0)
hertz(0) = loops(0)/(time_thus_far/1000)
ENDIF

Hertz$ = "PC@"+STR$(hertz(0))+"Hz"
LOOP

`Hertz$ is valid after 1 second real time.  This is needed to prevent a division by zero.