8

In my application I am executing a new .NET Thread and within that thread I am acomplishing a task.

I am using Stopwatch to measure the execution time but Stopwatch measures the execution time of all threads of OS (nut just the execution time for my thread). I want a way to measure just the time that my created thread takes to execute its own instructions.

Is there such a way of measuring in .NET?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
drill
  • 115
  • 1
  • 5
  • 2
    Check http://stackoverflow.com/questions/7598810/precisely-measure-execution-time-of-code-in-thread-c – mostruash Apr 20 '14 at 13:32

2 Answers2

5

There is no way to do this in managed code only, but you can PInvoke QueryThreadCycleTime or GetThreadTimes. There is one thing to keep in mind - there is no requirement that there must be a one to one relationship between managed and native threads but as far as I know this is the way it currently works. Using Stopwatch you will always get the elapsed wall clock time including time when your thread was suspended.

Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
  • Searching on google using your suggestion GetThreadTimes I found this article in Codeproject http://www.codeproject.com/Articles/31152/ExecutionStopwatch. So, you think that this calculates just my thread`s execution time? – drill Apr 20 '14 at 14:09
  • Yes, this should return the execution time recorded by the operating system for the particular thread. – Daniel Brückner Apr 20 '14 at 14:30
  • I just implemented it. Now, I have a doubt. I may be wrong but if I execute the same algorithm with the same inputs several times, shouldnt the GetThreadTimes give me always the same amount of time? Because each time it gives me different time value. The difference is very small but according to me it should give me the same time value always !! Maybe I am wrong. – drill Apr 20 '14 at 14:39
  • When it first executes the JIT compiler will get invoked and so the measured time may include time used to JIT the code. Cache hits and misses will also influence the execution time - while you read a value from L1 cache in one or a few cycle it takes hundreds of cycles to get the value from RAM, millions of cycles to get it from disc when swapped out. Getting exactly the same time repeatedly is very improbable in reality. – Daniel Brückner Apr 20 '14 at 14:44
  • Thank you @DanielBrückner very much ^_^ – Abdul Fattah Boshi Aug 15 '18 at 22:25
-1

This code measures the execution time of a thread:

void ThreadMethod() {
    Stopwatch sw = Stopwatch.StartNew();
    SomeActions();
    vat time = sw.Elapsed;
}

Run the method within your thread and you will get your execution time.

AgentFire
  • 8,944
  • 8
  • 43
  • 90
  • 5
    ...but let`s suppose that while the thread is being executed, a new apllication is started from OS (a large application which overloads the cpu). In this case the thread will also stop working for some "ticks" of CPU. Will the stopwatchmeasure the proper time of thread in this case, or it will be affected from the other threads of CPU? – drill Apr 20 '14 at 13:40