I have a performance problem. I create 100 new buttons and I want to assign an Click Event Handler. I execute this code for about 100 times:
Buttons[i].Button.Click += new System.EventHandler(Button_Click);
It takes about 2sec to complete. I have a lot of other event assignments in the same function, but they all take only some millisecond to execute. So I have transformed my code in
Buttons[i].Button.MouseUp += new System.Windows.Forms.MouseEventHandler(Button_Click);
Now the code is fast (some millisecond, like the others). Obviously I have modified the parameters of the function “Button_click” to fit the new event requirements, but no other changes were made.
I am wondering why this could happen. Is EventHandler that slow? Or am I doing something wrong? Or is there a best practice?
I am using VC2010 with C#, using .NET 4 in a Windows Form application.
EDIT:
Now I have “minified” my code and I put it there:
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Button b;
for(n=0;n<100;n++)
{
b = new Button();
b.Location = new System.Drawing.Point(100, 0);
b.Name = "btnGrid";
b.Size = new System.Drawing.Size(50, 50);
b.Text = b.Name;
b.UseVisualStyleBackColor = true;
b.Visible = false;
b.Text = "..";
b.Click += new EventHandler(this.Button_Click);
//b.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Button_ClickUP);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Log(elapsedTime, Color.Purple);
Button_Click and Button_Click are:
private void Button_Click(object sender, EventArgs e)
{
}
private void Button_ClickUP(object sender, MouseEventArgs e)
{
}
I put this code in a button and the “Log” function display the result in a memo. When I enable “Click” the result is 01.05 sec, but when I enable “MouseUp” the result is 00.00.
Difference -> ONE SECOND!
why!?
== EDIT ==
I use .NET Framework 4. VS2010. Win XP. I found this: if I use .NET 3.5 or lower the speed changes: 0.5 sec. An Half.
If I compile in debug or release mode it doesn’t change.
If I use the executable without the debugger is blazing fast.
So I change my question: is .NET 4 slower then .NET 3? Why the Release mode works differently compared to the stand alone version?
Many thanks.
The code “.Click += …” is transformed into “.add_Click( … )”. The “add_Click” method can have some logic checks.
You can little-bit speed up with no recreation of delegate:
EDIT:
Are you sure, the bottleneck is the attaching the handlers?
I tried the for loop (100 loops) with attaching the eventhandler to Click event and I get this results:
The source code:
EDIT 2:
I tried compare time spent with attaching Click handler vs. attaching MouseUp handler. It does not seems, the attaching MouseUp event is faster than Click event.
I think the problem will be somewhere else. Don’t GC collect during your loop? Or don’t you do something else there?
Results:
Code:
EDIT :
The body of
add_Clickmethod (=Click += ...) is rough:The MouseUp events will looks similar. At least both events using
Eventsproperty for holding lists of delegates for events.But if I tried several things I can not get the problems with the events as you wrote :(.
Can you reproduce same behaviour on another computers?