在C#中测量函数的执行时间 [英] Measuring the execution time of a function in C#

查看:91
本文介绍了在C#中测量函数的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测量函数的执行时间,我们得到以下值:

Measuring the execution time of a function, we get the following values:

1035, 1015, 1020, 1025, 1010



什么是函数的平均执行时间吗?



我尝试过:



我使用了平均执行公式





1035 + 1015 + 1020 + 1025 + 1010/5 = 1020


但是这里的答案错了我用过的程序





这里是C#程序


What is the average execution time of the function?

What I have tried:

I have used the Average Execution formula


1035+1015+1020+1025+1010 / 5 = 1020

But the Answer is Wrong here The program what i have used


Here is the C# program

Stopwatch sw = new  
Stopwatch();
sw.Start();
f();
sw.Stop();
Console.WriteLine(sw.Elapsed);

推荐答案

使用 .TotalMilliseconds 。如果你想要秒数,则除以1000(1000毫秒= 1秒)...



Use .TotalMilliseconds. Divide by 1000 (1000 milliseconds = 1 second) if you want number of seconds...

Stopwatch sw = new Stopwatch();
sw.Start();
f();
sw.Stop();
Console.WriteLine(


{sw.TotalMilliseconds / 1000 }秒);


这篇关于在C#中测量函数的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆