c# - 如何知道c#中的循环持续时间

标签 c# time

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.

8年前关闭。




Improve this question




我在 c# 中搜索计算循环持续时间的方法或任何类似的东西,认为我的程序包含 for 或 while “for(...,...,...){}” 我需要方法输出的类型需要多少时间

最佳答案

使用 Stopwatch Class

Provides a set of methods and properties that you can use to accurately measure elapsed time.


    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    // stuff you want to time here...

    stopWatch.Stop();

    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value. 
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
    Console.WriteLine("RunTime " + elapsedTime);

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

关于c# - 如何知道c#中的循环持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18427160/

相关文章:

c# - 使用 Linq to Entities 插入现有行的副本

c# - IEnumerable <List <DataRow >>不包含ToList()的定义

c# - 将方法指针从 C# 传递到 Delphi DLL

c# - 以编程方式删除文件并将其上传到 Azure Web 应用本地存储

c# - 如何使用带有单个查询的 EF 从 MSSQL 选择 +1 记录?

go - time.Location()返回“Local”,即使/etc/localtime是正确的

mysql - 它表示什么时间?

r - 如何将数字转换为 hh :mm:ss?

node.js - 如何使用nodejs从系统中获取本地时区

php - 将时间分成间隔