.net秒表的正确性(Correctness of .net stopwatch)

编程入门 行业动态 更新时间:2024-10-20 00:30:20
.net秒表的正确性(Correctness of .net stopwatch)

我们正在调试一些性能问题,并注意到秒表的一些奇怪结果。

我们有一个调用Web服务的客户端 我们使用秒表在Web服务的服务层记录时间 我们使用datetime.now.ticks记录下层的时间

第2层只是一个通过层,有2行日志记录。

记录的时间是:

110毫秒 52125毫秒 125毫秒

我们原本预计2小于1,尽管没有api精确到1 ms。

我们在每个服务电话中创建一个新的秒表,所以这不是旧秒表重新启动的时间。

有谁知道为什么我们得到这些数字?

编辑

2和3位于同一台计算机和相同的应用程序域中

秒表代码是:

var sw = new Stopwatch(); sw.Start(); //code to call layer 3 sw.Stop(); orchestrationContext.LogOperationTime(functionName, sw.ElapsedMilliseconds);

在第3层记录时间

protected DateTime StartTime { get; set; } StartTime = DateTime.Now; // code new TimeSpan(DateTime.Now.Ticks - StartTime.Ticks).Milliseconds

1的日志记录位于单独的计算机上,使用Websphere进行日志记录。

We are debugging some performace problems and have notice some strange results from stopwatch.

We have a client that calls a web service We log the time at the service layer og the web service with stop watch We log the time at the lower layer with datetime.now.ticks

Layer 2 is just a pass through layer with 2 lines of logging.

The times recorded are:

110 ms 52125 ms 125 ms

We had expected 2 to be less than 1, although no api is accurate to 1 ms.

We create a new stopwatch in each service call so this is not the time of an old stopwatch thar has been restarted.

Anyone know why we are getting these figures?

Edit

2 and 3 are on the same machine and the same app domain

The stopwatch code is:

var sw = new Stopwatch(); sw.Start(); //code to call layer 3 sw.Stop(); orchestrationContext.LogOperationTime(functionName, sw.ElapsedMilliseconds);

logging of time in layer 3

protected DateTime StartTime { get; set; } StartTime = DateTime.Now; // code new TimeSpan(DateTime.Now.Ticks - StartTime.Ticks).Milliseconds

The logging of 1 is on a seperate machine, logging using Websphere.

最满意答案

好的,你的第二个代码示例应该返回TimeSpan.TotalMilliseconds ,使用Milliseconds你将得到第二个子部分。

为什么不在第3层代码中使用StopWatch ,与第2层代码的样式相同,它更准确?

你的问题标题是“StopWatch的正确性”,但似乎你正在比较香蕉与桃子和苹果。 如果您的硬件或操作系统支持, StopWatch将使用HighResolution计时器,否则它将回HighResolution System.Timer ,它提供大约15ms的粒度。

我无法评论如何或是否使用WebSphere,但它不会提供比StopWatch更准确的时序,除非它使用API​​来处理其他一些高精度时间源。

Okay, your second code sample should be returning TimeSpan.TotalMilliseconds, using just Milliseconds you will get the sub second part.

Why aren't you using a StopWatch in the layer 3 code, in the same style as your layer 2 code, it is more accurate?

Your question title is "Correctness of StopWatch" but it seems you are comparing bananas with peaches and apples. StopWatch will use a HighResolution timer if supported by your hardware or operating system otherwise it will fallback to a System.Timer which offers a granularity of around 15ms.

I can't comment on how or whether to use WebSphere but it will not offer more accurate timing than StopWatch unless it uses an API to some other high accuracy time source.

更多推荐

本文发布于:2023-07-06 07:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:秒表   正确性   net   Correctness   stopwatch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!