如何在C#中测量线程的时间?

8
我想要测量一个C#例程所需的时间。因为有许多其他线程,我只想计算这个线程的时间。在Java中,我可以使用getCurrentThreadCpuTime
我该怎么做呢?

https://dev59.com/6XNA5IYBdhLWcg3wgeEd - Carsten
http://www.pinvoke.net/default.aspx/kernel32.getthreadtimes - Mike Clark
3个回答

1

我看不出这应该如何仅针对当前线程进行测量。 - Horcrux7

1
你不能。你无法测量特定线程在CPU上累计的时间。最准确的方法是为每个任务拆分一个单独的进程,然后测量该进程的CPU时间(实际上可以在.Net中完成)...但这有点过头了。如果你需要帮助,请提出另一个专门针对此问题的问题。

-3
你可以使用计时器来实现。这是最简单的方法。
    public void Worker()
    {
        var stopwatch = new Stopwatch();

        stopwatch.Start();

        ///Do your wwork here

        var timeElapsed = stopwatch.Elapsed;
    }

更新

我理解错了你的问题,那么这个怎么样?如果使用线程睡眠,它不起作用。如果还不是你要找的,请原谅。

    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();
        }
    }

1
-1 这不是 OP 所问的。 - tnw
你在这里测量的是一个操作,而不是一个线程,因此这个答案不是 OP 所寻找的。 - L-Four
OP要求获取特定线程的执行情况,而你只是在测量一系列操作执行所需的时间。这与测量特定线程无关。虽然类似,但并非他所要求的。 - tnw
这似乎计算了所有并行线程的时间,而不是单个线程的 CPU 时间。 - Horcrux7
这只是测量两个操作之间的时间,它不测量单个线程做任何事情所花费的时间。 - Peter Ritchie
@PeterRitchie,我不明白你的意思。截至今天,你没有任何方法/属性可以获取线程完成所需的时间,因此我的示例是非常有效的实现。如果你有更好的方法,请告诉我们,否则对我的答案进行投票并不能帮助OP AFAIK。 - MeTitus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接