如图,这是我在visual stdio中的结果
在 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);
}
}
这个版本的配置非常方便且好操作
下载完成之后出现的是以下页面。