1

Is there any performance overhead in using RaiseEvent in .NET?

I have a code which is similar to following.

Dim _startTick As Integer = Environment.TickCount
'Do some Task'
Dim duration As Integer = Environment.TickCount - _startTick
Logger.Debug("Time taken: {0}", duration)
RaiseEvent Datareceived()

The above code returns:

Time taken: 1200

Time taken: 1400

But if I remove RaiseEvent it returns:

Time taken: 110

Time taken: 121

I am surprised that the RaiseEvent is called after the logging of time taken. How does it affect the total time taken?

I am working on .NET Compact Framework.

In the Eventhandler I had given a MsgBox. When I removed the message box, it is now showing time taken as 110, 121, etc., that is, less that 500 milliseconds. If I put the Msgbox back in eventhandler it shows 1200, 1400, etc., that is, more than a second.

I am more surprised now (the event is raised after the logging part).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sachin Chavan
  • 5,578
  • 5
  • 49
  • 75
  • Maybe the `RaiseEvent` is the only statement that does anything with the received data, so if you remove it, (part of) the computation is optimized away? – Thomas May 03 '10 at 07:20
  • Event handler of this Raised event just shows a Msgbox. Further update if I remove the Msgbox and put Logger.Debug performance is improved. The main point is RaiseEvent is called after the logging then how it effects something that is already executed. – Sachin Chavan May 03 '10 at 07:26
  • It shouldn't be affecting previously executed code. As Time Taken is over 1 second, you might want to try step through the 'Some task' to see if there's any operation there taking all that time. – C.Evenhuis May 11 '10 at 13:40
  • @deltreme : It is the only code shown above. No 'some task' is done. I am more surprised for the same that it is affecting previously executed code. – Sachin Chavan May 20 '10 at 08:51

1 Answers1

1

Try using the same in a console application. A console application uses fewer resources.

Here you can identify the exact issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aarti
  • 26
  • 2