easy one

  • Thread starter Thread starter cj
  • Start date Start date
C

cj

I need to get the time it took for a process to run. It should be less
than a second but may be up to 5 seconds.

I'm used to showing how many seconds something has taken by:

starttime = now()
..
..
finishtime = now()
label1.text = DateDiff(DateInterval.Second, starttime, finishtime)

but I don't think this will show me fractions of a second. How can I
get it to display fractions of a second?
 
You could do something like:

label1.text = finishtime.Subtract(starttime).TotalSeconds.ToString()
 
Hello, cj,

Re:
Also, be aware that using "Now" will become very inaccurate if the
durations that you are measuring are less than 10-30 milliseconds.
Depending on your need for accuracy, you might want to investigate the
QueryPerformanceCounter API.

Cheers,
Randy
 
Hi, Cor,

Thanks for the update and the link. Yes, there is no "absolute"
accuracy -- even on a real-time OS. (And Windows doesn't even pretend
to be real-time.)

I guess that the real question is: "Is it accurate enough?". I think
that the Now function might not be accurate enough for the needs of the
OP -- but then again, it might. QueryPerformanceCounter will probably
be accurate enough, but it is not available on all systems and there are
a few subtleties involved in using it.

It looks like StopWatch is basically a wrapper for whatever tool
provides the greatest accuracy. (This is probably
QueryPerformanceCounter on most systems, but TickCount on others). This
is certainly easier than having to code for both possibilities like I
had to do in VB6. I'm still using the earlier version. Too bad it's
not available there too.

Groetjes,
Randy
 
Back
Top