我们正在调试一些性能问题,并注意到秒表的一些奇怪结果。
我们有一个调用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).Milliseconds1的日志记录位于单独的计算机上,使用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.ticksLayer 2 is just a pass through layer with 2 lines of logging.
The times recorded are:
110 ms 52125 ms 125 msWe 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).MillisecondsThe 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.
更多推荐
发布评论