c# - 花时间运行程序

标签 c# performance time

如何在程序运行时在文本框或输出中打印?

我希望将它显示到一个 for 循环中,以获得每个 for 循环需要多少时间。

最佳答案

你可以试试:

DateTime dt = DateTime.Now;
for (.......)
{
}
TimeSpan ts = DateTime.Now - dt;
textbox1.Text = ts.TotalMilliseconds.ToString();

或者(根据 MSDN )如果您需要更好的分辨率

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (.......)
{
}
stopWatch.Stop();
textbox1.Text = stopWatch.ElapsedMilliseconds.ToString();

关于c# - 花时间运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7836508/

相关文章:

algorithm - 确定最大开放空间的高效算法

Mysql 处理数十亿行的能力

go - 在golang中将时间戳转换为ISO格式

c# - 如何在SSRS中传递参数

c# - 如何将 xaml 中的值分配给不可绑定(bind)的属性

mysql - 增加对mysql的同时请求数

python - 安排具有单调时间的精确时间

c++ - gmtime 返回不正确的时间

c# - 使用 JSON.net 将枚举容器序列化为字符串

c# - 使用 ElemMatch 的 ArgumentNullException