views:

564

answers:

2

Hi,

What is the most accurate way of timing a thread or a line of code in C# assuming the application is multithreaded?

Kind regards,

+5  A: 

What exactly do you mean by "timing a thread"?

To just time (in wall time) how long something takes, use System.Diagnostics.Stopwatch. I don't believe there's anything to measure the processor time taken by a particular thread. Of course, profilers will help you a lot, but they also affect the timing of the program they're examining, which makes things trickier.

Jon Skeet
The problem is we have got around 20 threads and want to know the processor tine taken by each thread.
SharePoint Newbie
Is there no way that you can isolate the threads to benchmark each individually? You could use a profiler to get an idea of what's taking a long time within your threads, but that *will* affect the overall performance. (More for the more accurate instrumentation measures, less for sampling.)
Jon Skeet
+2  A: 

If you need to accurately time operations in .NET, you want the Stopwatch class (which wraps the Windows QueryPerformanceCounter API). Check out this post for thread timing considerations.

Mitch Wheat