bbg wrote:
I am working on window service application in which threads come and go
and found ManualResetEvent was created but not used(nobody waiting).
So I became curious how creating unnecessary ManualResetEvent will affect
performance.
About "using kernel stuff in thread is not good..",
http://msdn.microsoft.com/msdnmag/is...urrentAffairs/
By the way, I went ahead and downloaded the code from that article so I
could try it myself. Unfortunately, Richter doesn't actually state the
specific results he got, however I was unable to reproduce a significant
difference in performance.
In fact, with the exact code available for download, while his
implementation is in fact faster for a task that doesn't actually do
anything, the built-in CLR implementation of the async processing is
_faster_ than his "don't create the event handle" version when the task
actually takes some time to complete.
On my PC, in the 0-time case for 100,000 task iterations, I get about
1.4 seconds for his implementation and 4.4 seconds for the CLR
implementation. His wins that round.
But when I change it so that the task length is 1 second, reducing the
iterations to 1000 (otherwise it takes too long to complete the test :)
), his version takes 47 seconds while the CLR takes only 40 seconds.
If I do 1000 iterations of 100 ms-long tasks (this required changing his
LongTask class so that I could initialize it using a TimeSpan instead of
with seconds, of course), then his implementation takes about 10.3
seconds while the CLR only takes 4.3 seconds.
Out of three different scenarios, his implementation wins only one,
while the CLR wins the other two. I suspect the CLR would win _any_
scenario in which the task thread doesn't complete immediately.
This is on a Core 2 Duo, 2.33Ghz computer running with Windows XP SP 2
and .NET 2.0.
As much as I respect what Richter writes elsewhere, I don't see any
strong evidence here that creating an event handle really impedes
performance. It seems to me that he hasn't really measured performance
in an interesting or useful way.
Even if creating an event handle does have some significant overhead
(and IMHO that's far from proven by the code for that article), whatever
difference exists is easily swamped by other performance differences
elsewhere in the code (for example, the context switching forced by
making the task threads block...and IMHO if you've got something that
costs less than a context switch, it's probably not worth worrying about
unless you've got a task that can complete in a single timeslice).
Pete