Page 4
Timing an Event in DataFlex
by Curtis Krauskopf
A Better
Timer
Despite
the limitations of the SYSDATE and SYSDATE4 commands,
we can at least get consistent results when timing events.
As
demonstrated by the diagrams
in the previous article, one of the problems is
that the program doesn't know at what fraction of a
second it started. In the following diagrams, if we
could find out how much time was in the shaded sections,
both events would be reported (accurately) as having
finished in the same amount of time.
Even
though we can't determine exactly when the program started,
we can control when the event starts. We could have
the program delay the start of the event until the start
of the next second. At that point, the event would immediately
start and then both events would finish at the same
time.
Graphically,
the result looks like this:
An
example program that does this is:
// delay.src
// by Curtis Krauskopf at www.decompile.com
//
// This is an improvement on howbored.src.
// In this program, we synchronize the system's
// clock with the program's execution. That way,
// no matter what fraction of a second has elapsed
// since the program started, the event will
// always start at the start of a second. This
// provides more reliable timing tests by solving
// the problem where the test might start immediately
// before the second rolls over, giving the false
// impression that the test took longer than it
// really did.
//
date before after
integer before_hour before_minute before_second
integer after_hour after_minute after_second
integer second
// This section synchronizes the program's execution
// with the system's clock. The execution will
// leave the repeat/until loop when the clock
// rolls over to the next second.
sysdate4 before before_hour before_minute second
repeat
sysdate4 before before_hour before_minute before_second
until second ne before_second
// Insert the delay being tested here...
showln "The clock has started ticking."
showln "When you get bored, press any key."
showln "..."
string akey 1
inkey akey // This is the delay being tested
// Immediately after the delay, record the system time.
sysdate4 after after_hour after_minute after_second
number before_time // Accumulated number of seconds
number after_time
move (integer(before) * 3600 * 24) to before_time
move (before_time + (before_hour * 3600)) to before_time
move (before_time + (before_minute * 60)) to before_time
move (before_time + (before_second)) to before_time
move (integer(after) * 3600 * 24) to after_time
move (after_time + (after_hour * 3600)) to after_time
move (after_time + (after_minute * 60)) to after_time
move (after_time + (after_second)) to after_time
show "It took you " (after_time - before_time)
showln " seconds to become bored."
showln
showln "Press any key to continue."
inkey akey
|
One
significant difference between this program and the
howbored.src
program is that when this program is executed, there
may be a pause of up to almost one second before the
message
displays.
During this delay, the program is synchronizing itself
with the system clock. The message only displays when
the clock rolls over to a new second.
Copyright
2003 The Database Managers, Inc.
|