By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

System.Timers.Timer performance test ?

P: n/a
Hello
I'm testing the performance of the System.Timers.Timer class.
I created a small program that create 100 User objects.
Each USer object create a MyTimer object.

The constructor of the User class contains a name and a time to wait
for the timer.
Calling start on the User objet initiate the MyTimer to callback in 1
second.

When the User received the callback from MyTimer, it restart a new
timer for 1 second.

It works fine with 10 users but with 100 users and more, It seems that
few User callbacks
do not appears.

With 10 users, each second the total is : 10, 20, 30, 40, 50, ....
With 100 users the total is : 100, 200, 270, 365, .....
It's not regular !!!

Have you an idea ?
What will it be the best implemation to do that ?

Thanks

The c# program :

using System;

namespace TimersPerf
{
public delegate void MyTimerHandler (double delta);
class MyTimer
{
public event MyTimerHandler CallBack;
System.Timers.Timer t=null;
private DateTime startTime, endTime;
private int ms;

public MyTimer(int ms)
{
this.ms = ms;
}

public void Set()
{
if (t==null)
{
t = new System.Timers.Timer(ms);
t.AutoReset = false;
t.Elapsed+=new System.Timers.ElapsedEventHandler(t_Elapsed);
startTime = DateTime.Now;
t.Start();
}
}

private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
endTime = DateTime.Now;
TimeSpan delta = endTime-startTime;
t.Stop();
t.Dispose();
t = null;
CallBack(delta.TotalMilliseconds);
}

}

class User
{
private MyTimer t;
private int name;
private int ms;
private int cpt =0;

private static object obj = new object();
private static int total = 0;

public User(int name, int ms)
{
this.name = name;
this.ms = ms;
t = new MyTimer(ms);
}

public void start()
{
t.CallBack+=new MyTimerHandler(CallBack);
t.Set();
}

private void CallBack(double delta)
{
cpt++;
lock(obj)
{
total++;
Console.WriteLine("name={0}, delta={1}, cpt={2}, total={3}", name,
delta, cpt, total);
}

t.Set();
}
}


class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.foo();
Console.ReadLine();
}
public void foo()
{
int count = 10;
User[] user = new User[count];
for(int i=0; i<count; i++)
{
user[i] = new User(i, 1000);
user[i].start();
}
}

}
}

Nov 28 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

The WinXX OS is not a real time OS , therefore you cannot be 100% sure that
an event will fire at a given time.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<lo******@hotmail.fr> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello
I'm testing the performance of the System.Timers.Timer class.
I created a small program that create 100 User objects.
Each USer object create a MyTimer object.

The constructor of the User class contains a name and a time to wait
for the timer.
Calling start on the User objet initiate the MyTimer to callback in 1
second.

When the User received the callback from MyTimer, it restart a new
timer for 1 second.

It works fine with 10 users but with 100 users and more, It seems that
few User callbacks
do not appears.

With 10 users, each second the total is : 10, 20, 30, 40, 50, ....
With 100 users the total is : 100, 200, 270, 365, .....
It's not regular !!!

Have you an idea ?
What will it be the best implemation to do that ?

Thanks

The c# program :

using System;

namespace TimersPerf
{
public delegate void MyTimerHandler (double delta);
class MyTimer
{
public event MyTimerHandler CallBack;
System.Timers.Timer t=null;
private DateTime startTime, endTime;
private int ms;

public MyTimer(int ms)
{
this.ms = ms;
}

public void Set()
{
if (t==null)
{
t = new System.Timers.Timer(ms);
t.AutoReset = false;
t.Elapsed+=new System.Timers.ElapsedEventHandler(t_Elapsed);
startTime = DateTime.Now;
t.Start();
}
}

private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
endTime = DateTime.Now;
TimeSpan delta = endTime-startTime;
t.Stop();
t.Dispose();
t = null;
CallBack(delta.TotalMilliseconds);
}

}

class User
{
private MyTimer t;
private int name;
private int ms;
private int cpt =0;

private static object obj = new object();
private static int total = 0;

public User(int name, int ms)
{
this.name = name;
this.ms = ms;
t = new MyTimer(ms);
}

public void start()
{
t.CallBack+=new MyTimerHandler(CallBack);
t.Set();
}

private void CallBack(double delta)
{
cpt++;
lock(obj)
{
total++;
Console.WriteLine("name={0}, delta={1}, cpt={2}, total={3}", name,
delta, cpt, total);
}

t.Set();
}
}


class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.foo();
Console.ReadLine();
}
public void foo()
{
int count = 10;
User[] user = new User[count];
for(int i=0; i<count; i++)
{
user[i] = new User(i, 1000);
user[i].start();
}
}

}
}

Nov 28 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.