8

I want to measure the time that a C# routine needs. Because there are many other threads I only want to count the time of this one thread. In Java I can use getCurrentThreadCpuTime.

How can I do it?

Carsten
  • 11,287
  • 7
  • 39
  • 62
Horcrux7
  • 23,758
  • 21
  • 98
  • 156

3 Answers3

1

You should look into PerformanceCounters. They are quite complex and can be a bit of a pain to set up, but are robust in what they offer for metrics. A couple of things that might help:

Performance counters and threading

http://blogs.msdn.com/b/adamhems/archive/2008/12/04/using-custom-performance-counters-to-measure-multi-threaded-operation-durations.aspx

Community
  • 1
  • 1
AJ.
  • 16,368
  • 20
  • 95
  • 150
1

You can't. You cannot measure the accumulated time ON THE CPU for a particular thread. The most accurate thing you could do would be to spin off a separate process for each of your tasks, and then measure the CPU time for the process (which actually can be done in .Net)... but that's overkill.

If you need help on how to do that, you should ask another question specifically for that.

eMi
  • 5,540
  • 10
  • 60
  • 109
-3

You can use Stopwatch for that. It would be the simplest way to get that.

    public void Worker()
    {
        var stopwatch = new Stopwatch();

        stopwatch.Start();

        ///Do your wwork here

        var timeElapsed = stopwatch.Elapsed;
    }

UPDATE

I got your question wrong, so what about this? It does not work if you use thread sleep. Sorry if this still is not what you're looking for.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;
    using System.Runtime.InteropServices;
    using System.Collections.Concurrent;

    namespace ConsoleApplication2
    {
        class Program
        {
            static ConcurrentDictionary<int, ProcessThread> threadIdsMapping = new ConcurrentDictionary<int, ProcessThread>();


            static void Main(string[] args)
            {
                Thread oThread = new Thread(
                    delegate()
                    {
                        threadIdsMapping.GetOrAdd(Thread.CurrentThread.ManagedThreadId, GetProcessThreadFromWin32ThreadId(null));

                        long counter = 1;

                        while (counter < 1000000000)
                        {
                            counter++;
                        }
                    });

                oThread.Start();
                oThread.Join();

                Console.WriteLine(threadIdsMapping[oThread.ManagedThreadId].TotalProcessorTime);
                Console.WriteLine(threadIdsMapping[oThread.ManagedThreadId].UserProcessorTime);
                Console.WriteLine(DateTime.Now - threadIdsMapping[oThread.ManagedThreadId].StartTime);

                Console.ReadKey();
            }

            public static ProcessThread GetProcessThreadFromWin32ThreadId(int? threadId)
            {
                if (!threadId.HasValue)
                {
                    threadId = GetCurrentWin32ThreadId();
                }

                foreach (Process process in Process.GetProcesses())
                {
                    foreach (ProcessThread processThread in process.Threads)
                    {
                        if (processThread.Id == threadId) return processThread;
                    }
                }

                throw new Exception();
            }

            [DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
            public static extern Int32 GetCurrentWin32ThreadId();
        }
    }
MeTitus
  • 3,390
  • 2
  • 25
  • 49
  • 1
    -1 that's not what OP asked. – tnw Apr 24 '13 at 13:13
  • You are measuring an operation here, not a thread, so this answer is not what OP is looking for. – L-Four Apr 24 '13 at 13:16
  • OP asked to get the execution of a *specific thread*, you're simply measuring the time it takes for a series of operations to execute. This has nothing to do with measuring a particular thread. This is similar, but not what he asked for. – tnw Apr 24 '13 at 13:19
  • This seems to count the time of all parallel threads and not the CPU time of the single thread. – Horcrux7 Apr 24 '13 at 13:25
  • This simply measures the time between two operations, it doesn't measure how much time a single thread spent doing anything. – Peter Ritchie Apr 24 '13 at 14:35
  • @PeterRitchie I don't get you. You don't have any way, as of today of getting the time a thread took to complete using a simple method/property, so my example is a very valid implementation, and if you have a better way of doing it, let us know, down voting my answer does not help the OP AFAIK. – MeTitus Apr 24 '13 at 14:42