7

I have a question about the usage of System.Timers.Timer in my .NET applications.

I have three different events, one triggers every second, another triggers every minute and the last one triggers every half hour.

Is it better, performance wise, to have one timer or three?

The way to do it with one timer is to check ElapsedEventArgs' SignalTime and compare it to a previously measured time. Of course this doesn't quite work if you've changed the DateTime, but that's not the issue here. Basically every second it elapses I check whether a minute or a half hour has passed as well...

My concern is that using 3 timers creates too much overhead. Am I wrong and should I use 3 for simplicity and clarity or is 1 timer enough?

Code:

private void TimerEverySecondElapsed(object sender, ElapsedEventArgs e)
    {
        timerEverySecond.Enabled = false;
        OnTimerElapsed(TimerType.EverySecond);

        // Raise an event every minute
        if ((e.SignalTime - previousMinute) >= minuteSpan)
        {
            OnTimerElapsed(TimerType.EveryMinute);
            previousMinute = e.SignalTime;
        }

        // Raise an event every half hour
        if ((e.SignalTime - previousHalfhour) >= semiHourSpan)
        {
            OnTimerElapsed(TimerType.EverySemiHour);
            previousHalfhour = e.SignalTime;
        }

        timerEverySecond.Enabled = true;
    }

minuteSpan and semiHourSpan are readonly TimeSpans.

Davio
  • 4,609
  • 2
  • 31
  • 58
  • this doesn't answer the question, but another option is having two timers (one for seconds, one for the minute/30minutes). – Rotem Dec 06 '12 at 11:17
  • `Is it better, performance wise, to have one timer or three` only you can answer that. Is this a bottleneck of you app? – Lukasz Madon Dec 06 '12 at 11:18
  • **Performance won't be affected** in any measurable way if you use just one timer (two _if_ won't make your code slow, did you **profile** it?) and you'll save the overhead (in resources) of three timers. But what you have to consider is **how to execute** that operations (if you use one single timer you may need to run them in the pool because synchronous **sequential execution** may delay the last one too much). – Adriano Repetti Dec 06 '12 at 11:18
  • To say how much it will affect on performance, you need to profile it as stated in other comments (IMO, it won't affect). Having three timers will create racing conditions that may become really big headache later. – Leri Dec 06 '12 at 11:21
  • [What is the maximum number of timers a program can create?]: http://blogs.msdn.com/b/oldnewthing/archive/2009/08/27/9886147.aspx – Arsen Mkrtchyan Dec 06 '12 at 11:22
  • @lukas I did some profiling and found that a lot of time was spent in the ElapsedHandler, but that could be because it's checking whether the other events need to be fired as well. I read somewhere that underneath a shared timer is used as much as possible so maybe using 3 timers is simpler and less error prone. Will post my code. – Davio Dec 06 '12 at 11:22

3 Answers3

5

It's not going to make much of a difference either way. Regardless of the number of timers there's only one loop going on in the background. At it's least efficient you will be using 3 threads from the thread pool but there are a lot available so it's not a big deal. If it makes your code simpler to read I'd say go for it.

Keep in mind however this only applies to Threading.Timer (AKA Timers.Timer). Forms.Timer behaves a bit differently and it would probably would be better to just stick with one of those since it only operates on the UI thread so you may run into some sync issues.

Threading.Timer vs. Forms.Timer

Some More Resources:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/f9a1c67d-8579-4baa-9952-20b3c0daf38a/

How many System.Timers.Timer instances can I create? How far can I scale?

Community
  • 1
  • 1
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
0

3 Timers add very little overhead in comparison to 1, and the code will most likely be a whole lot better (in terms of readability and maintainability)

Neowizard
  • 2,981
  • 1
  • 21
  • 39
0

I think checks with if's are more of overhead than three timers, besides you can get in some weird situations trying to use one timer.

I'd go with three timers.

Mateusz
  • 2,287
  • 20
  • 28