【问题标题】:How can I measure the time of a thread in c#? [duplicate]如何在c#中测量线程的时间? [复制]
【发布时间】:2013-04-24 13:10:41
【问题描述】:

我想测量 C# 例程所需的时间。因为还有很多其他线程我只想统计这一个线程的时间。在 Java 中,我可以使用 getCurrentThreadCpuTime

我该怎么做?

【问题讨论】:

标签: c# multithreading performance


【解决方案1】:

您应该查看PerformanceCounters。它们非常复杂,设置起来可能有点麻烦,但它们为指标提供的功能很强大。有几件事可能会有所帮助:

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

【讨论】:

  • 我看不出这应该如何仅针对当前线程进行测量。
【解决方案2】:

你不能。您无法测量特定thread在 CPU 上的累积时间。 您可以做的最准确的事情是为您的每个任务分拆一个单独的process,然后测量进程的 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 这不是 OP 要求的。
    • 你在这里测量的是一个操作,而不是一个线程,所以这个答案不是 OP 想要的。
    • OP 要求执行特定线程,您只是在测量执行一系列操作所需的时间。这与测量特定线程无关。这很相似,但不是他要求的。
    • 这似乎是计算所有并行线程的时间,而不是单线程的CPU时间。
    • 这只是衡量两个操作之间的时间,而不是衡量单个线程做任何事情所花费的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2011-02-27
    相关资源
    最近更新 更多