Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure execution time of a thread in C#

Tags:

c#

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?

like image 974
drill Avatar asked Apr 20 '14 13:04

drill


People also ask

How do you calculate thread execution time?

There are two ways to measure elapsed execution time in Java either by using System. currentTimeinMillis()or by using System. nanoTime(). These two methods can be used to measure elapsed or execution time between two method calls or events in Java.

How do you find the time taken to execute a program in C?

To calculate time taken by a process, we can use clock() function which is available time.

How do we measure the execution time of programs?

The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.

How do I get runtime in C++?

measure execution time of a program. Using time() function in C & C++. time() : time() function returns the time since the Epoch(jan 1 1970) in seconds. Prototype / Syntax : time_t time(time_t *tloc);


1 Answers

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.

like image 166
Daniel Brückner Avatar answered Sep 29 '22 20:09

Daniel Brückner