Brian Mulcahy
Brian Mulcahy

Reputation: 1503

VB.NET Timing multiple things, better to have many stopwatches or one stopwatch and calculate offsets

VB.NET 2010, .NET 4

Hello,

I have an application which controls a process and several stopwatches that keep track of the elapsed time since various events.

The simplified picture is: The process starts, at a later time an event "A" occurs, at a later time an event "B" occurs, etc...

There are a finite number of such events. At the start of each event (including the process start event), I create and start a new stopwatch. I then update some indicators that display the amount of time since each event started.

So, I have a bunch of labels (LabelStart, LabelA, LabelB, etc) each formatted as HH:MM:SS which represent the elapsed time since each event occurred. Their text is derived from the corresponding stopwatchs' properties.

My question is, would it be better to have one stopwatch and a list of offset integers (from a CPU/memory efficiency standpoint)? I.e., the stopwatch starts at process start and, at each event, an integer equal to the current elapsed milliseconds on that stopwatch is added to a list. The labels could then be updated by subtracting the offset from the one running stopwatch.

I have no idea how they work. Maybe this is a dumb question. I'm just curious.

Thanks in advance! Brian

Upvotes: 0

Views: 617

Answers (2)

supercat
supercat

Reputation: 81189

The stopwatch type itself is a structure, rather than a class, and it essentially contains a "mode" indication along with a number that either represents the number of ticks elapsed (when it's not running), or the system performance counter value at which it should be deemed to have started (when it is running). An array holding a million StopWatch instances, all started at different times, would impose no more continuing overhead than any other array of similarly-sized structures.

Upvotes: 1

Tasawer Khan
Tasawer Khan

Reputation: 6148

If you are developing application for computers and if SEVERAL is not too much like less than 10 it should not make any difference. But the way you are thinking will make it more efficient.

Upvotes: 1

Related Questions