四水の世界 2023-04-27 15:37 采纳率: 33.3%
浏览 49

请问怎么查看visual stdio中代码的运行时间

如图,这是我在visual stdio中的结果

img


这是在dev c++中的结果

img


因为dev中不能运行大数据,转战visual stdio ,但是看不到运行时间,网上也没有搜到如何操作,所以提问,望不吝赐教……

  • 写回答

2条回答 默认 最新

  • Linbo Walking 2023-04-27 16:14
    关注

    在 Visual Studio 中,您可以使用 C++ 的 chrono 库或 C# 的 Stopwatch 类来测量代码的运行时间。这里分别给出 C++ 和 C# 的示例。

    
    #include <iostream>
    #include <chrono>
    
    int main() {
        // 开始计时
        auto start = std::chrono::high_resolution_clock::now();
    
        // 在此处编写您要测试的代码
        // ...
    
        // 结束计时
        auto end = std::chrono::high_resolution_clock::now();
    
        // 计算运行时间
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    
        std::cout << "代码运行时间为: " << duration << " 微秒" << std::endl;
        return 0;
    }
    
    C# 示例(使用 Stopwatch 类):
    
    csharp
    复制代码
    using System;
    using System.Diagnostics;
    
    class Program {
        static void Main() {
            // 创建 Stopwatch 实例并开始计时
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
    
            // 在此处编写您要测试的代码
            // ...
    
            // 停止计时
            stopwatch.Stop();
    
            // 输出运行时间
            Console.WriteLine("代码运行时间为: {0} 微秒", stopwatch.Elapsed.TotalMilliseconds * 1000);
        }
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 4月27日