- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在此示例代码中,我使用一个简单的秒表来测试完成给定任务/操作所需的时间
StopWatch SW1 = new StopWatch();
SW1.Start();
SomeAction();
SW1.Stop();
sting results = SW1.Elapsed.ToString();
MessageBox.Show(resutls);
我想要一个类,我将实例化以用于测试
public Class PerformanceTests
{
public StopWatch SW1 = new StopWatch();
public StopWatch SW2 = new StopWatch();
public string results1 = "", results2 = "";
....
....
//some other variables to use
}
尽管在实例化类并尝试使用 SW1
时不允许我使用它的方法。我做错了什么?
PerformanceTests Ptst = new PerformanceTests();
Ptst.SW1. ... Start() is not accessible
更新
对于其余的答案,不要复制我的代码,因为我错过了大写的 stopwatch
。我没有实例化 Stopwatch 类,而是不小心没有注意到 Visual Studio 询问我是否要为我所谓的秒表而不是 .NET 的真正 Stopwatch
创建一个类。
所以我的建议,注意Visual Studio intellisense的建议操作即使它应该一直都是一样的。只要确保您真正有经验并牢记所有类(class)即可。
最佳答案
这是一个简单的类,可以帮助您测量代码块执行的时间:
public class PerformanceTester : IDisposable
{
private Stopwatch _stopwatch = new Stopwatch();
private Action<TimeSpan> _callback;
public PerformanceTester()
{
_stopwatch.Start();
}
public PerformanceTester(Action<TimeSpan> callback) : this()
{
_callback = callback;
}
public static PerformanceTester Start(Action<TimeSpan> callback)
{
return new PerformanceTester(callback);
}
public void Dispose()
{
_stopwatch.Stop();
if (_callback != null)
_callback(Result);
}
public TimeSpan Result
{
get { return _stopwatch.Elapsed; }
}
}
用法(使用 PerformanceTester
包装代码块):
using (var tester = new PerformanceTester())
{
// code to test
MessageBox.Show(tester.Results.ToString());
}
如果您在 using
block 之前声明 tester 变量,那么秒表将在您退出 using
block 时自动停止,并且您可以得到结果:
PerformanceTester tester;
using (tester = new PerformanceTester())
SomeAction();
MessageBox.Show(tester.Results.ToString());
如果您将回调操作传递给 PerformanceTester
,则此操作将在 using
语句结束时调用,并将耗时传递给回调:
using (PerformanceTester.Start(ts => MessageBox.Show(ts.ToString())))
SomeAction();
您可以声明方法,它将接受TimeSpan
并处理结果:
private void ProcessResult(TimeSpan span)
{
// log, show, etc
MessageBox.Show(span.ToString());
}
用法变得非常干净:
using (PerformanceTester.Start(ProcessResult))
SomeAction();
关于c# - 使用 StopWatch 类进行性能测试的辅助类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13681664/
什么ElapsedTicks和 Elapsed.Ticks在 StopWatch 类中是什么意思?什么时候含义可能与预期不同? 最佳答案 我刚刚发现,如果 StopWatch.isHighResolu
我正在尝试将其他计时信息添加到 Symfony Profiler 时间轴中,但我无法显示任何内容。根据我读过的文档,它应该像下面的示例一样简单,但这不会导致时间轴上出现任何附加信息。 我是否需要以某种
我正在制作一个需要非常严格的计时的应用程序,而 Stopwatch 类是完美的解决方案。然而,我注意到有时,当在小型平板电脑上运行时,秒表值相差很大。我添加了一些调试打印输出,每 200 毫秒左右监控
我有一个用 JS 内置的秒表计时器,它使用小时、分钟、秒和毫秒从 0 开始计数。目前,当用户点击停止按钮时,计时器的值将附加到隐藏的输入元素以进行表单提交。我需要将任何时间转换为每小时,并将其附加到隐
我需要一个像 Stopwatch.GetTimestamp() 这样的时钟 获取值时的开销很小 精确到至少 1 毫秒(不减少操作系统范围的计时器间隔) 不必与一天中的实际时间 Hook (例如 Dat
我知道 DateTime 和 Stopwatch 类,但我有点困惑。我只是想知道哪个更好,为什么? 最佳答案 Stopwatch 类使用适合执行(某些)基准测试的高分辨率计时器。顾名思义,它测量耗时(
我有一个使用 GPU (NVIDA GTX980) 进行图像处理的 C# 和 .NET 应用程序。有 4 个阶段,我将 CPU 同步到 GPU(时间上没有重叠)以进行计时。但这些数字并没有加起来。 L
我有一个标记为[Serializable]的类,其中包含System.Diagnostics.Stopwatch成员。但是由于System.Diagnostics.Stopwatch,我无法使用Bin
我正在尝试在秒表实例上调用 Restart(),但在尝试调用它时出现以下错误: Assets/Scripts/Controls/SuperTouch.cs(22,59): error CS1061:
在此示例代码中,我使用一个简单的秒表来测试完成给定任务/操作所需的时间 StopWatch SW1 = new StopWatch(); SW1.Start(); SomeAction(); SW1.
如果我有一个共享的 System.Diagnostics.Stopwatch 实例,多个线程能否以安全的方式调用 shared.ElapsedTicks 并获得准确的结果? 以这种方式使用 Stopw
我还需要显示分钟,实际上我用这段代码来显示秒,但也需要分钟 TimeSpan ts = stopwatch.Elapsed; Console.WriteLine("File Generated: "
我正在尝试使用 Go 包“time”来对我程序中的函数进行基准测试。一直在四处寻找,人们似乎使用 time.NanoSeconds() - 但我认为这个功能不再包含在 time 包中? 我需要的是一种
我以前也这么认为 Stopwatch.ElapsedTicks 等于 Stopwatch.Elapsed.Ticks。 但事实并非如此。后者是 100 纳秒的周期,而前者则使用一个名为“计时器滴答”的
我正在尝试使用com.google.common.base.Stopwatch,但根据Eclipse,Stopwatch.createStarted()未定义。尝试编译时,我还在 createStar
我经常使用 System.Diagnostics.Stopwatch 来测量代码的执行时间。该文档说它应该非常准确。我相信测量时间与真实时间的差异仅在于用于启动和停止秒表的时间。 我通过下面的代码估计
鉴于 C# 中的 Stopwatch 类可以在下面使用类似三个不同计时器的东西,例如 系统定时器精度约为 +-10 ms,具体取决于可以使用 timeBeginPeriod 设置的计时器分辨率它可能约
最近在用guava,遇到这样一个问题想测一下put(str)方法执行的总时间,就是把字符串放到map中。我尝试使用 StopWatch 来执行此操作并计算方法执行的平均时间,但目前我不断返回 0 pr
我正在尝试从这个 jQuery Stopwatch 库获取毫秒值 https://github.com/robcowie/jquery-stopwatch 除了 GitHub 问题中指出的以外,毫秒值
Debug.WriteLine("Timer is high-resolution: {0}", Stopwatch.IsHighResolution); Debug.WriteLine("Tim
我是一名优秀的程序员,十分优秀!