SharePoint Newbie
SharePoint Newbie

Reputation: 6082

Timing a line of code accurately in a threaded application, C#

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

Kind regards,

Upvotes: 6

Views: 2746

Answers (2)

Mitch Wheat
Mitch Wheat

Reputation: 300817

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503859

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.

Upvotes: 8

Related Questions