Calculate processing time.

  • Thread starter Thread starter Qwert
  • Start date Start date
Q

Qwert

Hello,

I do:

Debug.WriteLine("A: " & DateTime.Now.Ticks.ToString)
REM Calculate a bunch of stuff. Some loops and math functions.
Debug.WriteLine("B: " & DateTime.Now.Ticks.ToString)

but both A and B have the exact the same value. Is this method incorrect?
When I make the function wait for 1 millisecond, A en B differ. Is there
another way to calculate the processing time? Without looping the stuff a
100 times and then devide the result by 100.

Thanks.
 
Debug.WriteLine("A: " & DateTime.Now.Ticks.ToString)
REM Calculate a bunch of stuff. Some loops and math functions.
Debug.WriteLine("B: " & DateTime.Now.Ticks.ToString)

but both A and B have the exact the same value. Is this method incorrect?
When I make the function wait for 1 millisecond, A en B differ. Is there
another way to calculate the processing time? Without looping the stuff a
100 times and then devide the result by 100.

I would prefer doing it 100 or even more times, to make sure you get an
accurate timing.

The first time you run a routine the CLR might do some optimizing that
actually makes it run slower the first few times.

/Søren Reinke
 
Qwert said:
Hello,

I do:

Debug.WriteLine("A: " & DateTime.Now.Ticks.ToString)
REM Calculate a bunch of stuff. Some loops and math functions.
Debug.WriteLine("B: " & DateTime.Now.Ticks.ToString)

but both A and B have the exact the same value. Is this method
incorrect? When I make the function wait for 1 millisecond, A en B
differ. Is there another way to calculate the processing time?
Without looping the stuff a 100 times and then devide the result by
100.


In addition to Søren, the resolution of DateTime.Now is too low to measure
things. Use Environment.Tickcount (returns milliseconds) instead or a high
performance counter (in VB 2005, you can use System.Diagnostics.Stopwatch)

See also:
http://groups.google.com/group/micr...nguages.vb/browse_frm/thread/bd7a1a5c8d980072

(especially post #4ff ;-)



Armin
 
Great, thanks all.

That 'QueryPerformanceCounter' and 'QueryPerformanceFrequency' code of
mentalis did the trick. None of the .NET functionality I could find was
precise enough (fastest time was 0,0003 seconds).

Don't have VB2005 yet, so I couldn't try "in VB 2005, you can use
System.Diagnostics.Stopwatch". I did try:

Dim objProcessorTime As New System.Diagnostics.PerformanceCounter()
objProcessorTime.CategoryName = "Processor"
objProcessorTime.CounterName = "% Processor Time"
objProcessorTime.InstanceName = "_Total"

but wasn't precise enough.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top