如何用c#来测量线程的时间?

如何用c#来测量线程的时间?,c#,multithreading,performance,C#,Multithreading,Performance,我想测量C#例程需要的时间。因为还有很多其他线程,我只想计算这一个线程的时间。在Java中,我可以使用getCurrentThreadCpuTime 我怎么做呢?你可以用秒表。这是最简单的方法 public void Worker() { var stopwatch = new Stopwatch(); stopwatch.Start(); ///Do your wwork here var timeElaps

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


我怎么做呢?

你可以用秒表。这是最简单的方法

    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();
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统诊断;
使用系统线程;
使用System.Runtime.InteropServices;
使用System.Collections.Concurrent;
命名空间控制台应用程序2
{
班级计划
{
静态ConcurrentDictionary threadIdsMapping=新建ConcurrentDictionary();
静态void Main(字符串[]参数)
{
线程oThread=新线程(
代表()
{
GetOrAdd(Thread.CurrentThread.ManagedThreadId,GetProcessThreadFromWin32ThreadId(null));
长计数器=1;
而(计数器<100000000)
{
计数器++;
}
});
oThread.Start();
oThread.Join();
WriteLine(threadIdsMapping[oThread.ManagedThreadId].TotalProcessorTime);
WriteLine(threadIdsMapping[oThread.ManagedThreadId].UserProcessorTime);
Console.WriteLine(DateTime.Now-threadIdsMapping[oThread.ManagedThreadId].StartTime);
Console.ReadKey();
}
公共静态ProcessThread GetProcessThreadFromWin32ThreadId(int?threadId)
{
如果(!threadId.HasValue)
{
threadId=GetCurrentWin32ThreadId();
}
foreach(Process.getprocesss()中的进程)
{
foreach(ProcessThread ProcessThread in process.Threads)
{
if(processThread.Id==threadId)返回processThread;
}
}
抛出新异常();
}
[DllImport(“Kernel32”,EntryPoint=“GetCurrentThreadId”,ExactSpelling=true)]
公共静态外部程序Int32 GetCurrentWin32ThreadId();
}
}
您应该查看。它们非常复杂,设置起来可能有点困难,但它们为度量提供了强大的功能。以下几点可能会有所帮助:


你不能。对于特定的
线程
,无法测量CPU上的累计时间。 您可以做的最准确的事情是为每个任务派生一个单独的
进程
,然后测量该进程的CPU时间(实际上可以在.Net中完成)。。。但这太过分了


如果您需要关于如何执行此操作的帮助,您应该专门问另一个问题。

-1 OP不是这样问的。您在这里测量的是一个操作,而不是一个线程,因此此答案不是OP想要的。OP被要求执行特定线程,您只是在测量执行一系列操作所需的时间。这与测量特定的螺纹无关。这是相似的,但不是他所要求的。这似乎是计算所有并行线程的时间,而不是单个线程的CPU时间。这只是测量两个操作之间的时间,而不是测量单个线程做任何事情所花费的时间。我看不出这只应该测量当前线程的时间。