Timing less than a second...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all..

I am trying to optimize some code that I have written. In order to do that, I would like to see how quickly certain parts of it run

I cannot figure out how to time an interval of less than a second. (Basically I'd like to try various different things to speed up the code, but would like to know the effects of each change...

In the past I've simply calculated the start time and end time using now(), and looked at the difference. But I need more than the 1 second that'll tell me

Any ideas

Thanks in advance

Greg Kaufman
 
Timer Function


Returns a Single representing the number of seconds elapsed since midnight.

Syntax

Timer

Remarks

In Microsoft Windows the Timer function returns fractional portions of a
second. On the Macintosh, timer resolution is one second.

Try the timer function by taking the difference of the timer function result
at strategic places in your code.

Maybe it would help by decalring the variables saving the timer result as
"double".

I haven't played with this in years, but it does indicate that it will
return "fractions" of a second...

Hope this helps
--


Frank Bachman
(Grumpy Aero Guy)


Greg Kaufman said:
Hello all...

I am trying to optimize some code that I have written. In order to do
that, I would like to see how quickly certain parts of it run.
I cannot figure out how to time an interval of less than a second.
(Basically I'd like to try various different things to speed up the code,
but would like to know the effects of each change...)
In the past I've simply calculated the start time and end time using
now(), and looked at the difference. But I need more than the 1 second
that'll tell me.
 
Greg

What you want is to use Windows built in Ms Timer. Add this declare to your
code.

Private Declare Function GetTime Lib "winmm.dll" Alias "timeGetTime" () As
Long

When called it returns the number of ms since last midnight as a long. So
at the start of the routine that you want to time you get the start time,
execute the code you want to time, and at the end of the section you are
testing make the call again and subtract the start time for the elapsed time
in Ms. For example:

lngStart = GetTime
'... your routine here
Debug.Print GetTime - lngStart & " Ms."

While this function returns time in Ms. I have found that it's accuracy is
on the order of Tens of Ms. It should be accurate enough to time functions
that take at least a half a second (500Ms). If the process you want to time
is smaller than that you might want to consider looping the routine a few
times. Good luck

Ron W

Greg Kaufman said:
Hello all...

I am trying to optimize some code that I have written. In order to do
that, I would like to see how quickly certain parts of it run.
I cannot figure out how to time an interval of less than a second.
(Basically I'd like to try various different things to speed up the code,
but would like to know the effects of each change...)
In the past I've simply calculated the start time and end time using
now(), and looked at the difference. But I need more than the 1 second
that'll tell me.
 
Greg said:
I am trying to optimize some code that I have written. In order to do that, I would like to see how quickly certain parts of it run.

I cannot figure out how to time an interval of less than a second. (Basically I'd like to try various different things to speed up the code, but would like to know the effects of each change...)

In the past I've simply calculated the start time and end time using now(), and looked at the difference. But I need more than the 1 second that'll tell me.


Here's the little function I use for this purpose:

Public Declare Function GetClock Lib "winmm.dll" _
Alias "timeGetTime" () As Long

Public Function Timing() As Long
Static lngPrevious As Long
Dim lngNewTime As Long
lngNewTime = GetClock()
Timing = lngNewTime - lngPrevious
lngPrevious = lngNewTime
End Function

I don't remember the exact resolution of API, but its a
relatively small number of milliseconds.

When you're timing your procedures, be sure to measure a
large number of repetitive executions to try to average out
the time taken by system interupts and both Disk and Ram
caching.
 
Back
Top