用于多处理器机器秒表最佳实践? [英] Best Practise for Stopwatch in multi processors machine?

查看:166
本文介绍了用于多处理器机器秒表最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个好问题测量功能的表现,答案推荐如下使用秒表

I found a good question for measuring function performance, and the answers recommend to use Stopwatch as follows

Stopwatch sw = new Stopwatch();
sw.Start();
//DoWork
sw.Stop();
//take sw.Elapsed



但如果你是多处理器下运行这是有效的机?该线程可以被切换到另一个处理器,可以吗?
也同样的事情应该是Enviroment.TickCount。
。如果答案是肯定的,我应该换行内BeginThreadAffinity我的代码如下:

But is this valid if you are running under multi processors machine? the thread can be switched to another processor, can it? Also the same thing should be in Enviroment.TickCount. If the answer is yes should I wrap my code inside BeginThreadAffinity as follows

Thread.BeginThreadAffinity();
Stopwatch sw = new Stopwatch();
sw.Start();
//DoWork
sw.Stop();
//take sw.Elapsed
Thread.EndThreadAffinity();



PS

P.S

可能出现操作过线程级不仅处理器级别,例如,如果该功能是在另一个线程运行,以便该系统可以切换到另一个处理器,如果出现这种情况,将秒表该切换

The switching can occur over the thread level not only the processor level, for example if the function is running in another thread so the system can switch it to another processor, if that happens, will the Stopwatch be valid after this switching?

我不使用秒表测量perfromance只,而且使用了Thread.Sleep模拟定时器功能(防止通话重叠)

I am not using Stopwatch for perfromance measurement only but also to simulate timer function using Thread.Sleep (to prevent call overlapping)

推荐答案

如果函数本身不是多线程(例如,它不产生其他线程/进程,并等待他们完成),那么唯一的问题是你的机器。

If the function itself isn't multithreaded (eg it doesn't spawn other threads/processes and wait for them to complete) then the only issue is your machine.

如果你的机器是忙着做其他事情也可能无效您的测试(如编码H.264视频同时做一个CPU绑定测试)。同样,如果你使用的所有物理内存上测试的东西是内存限制的话那可能无效的结果。

If your machine is busy doing other things it could invalidate your test (eg encoding a H.264 video while doing a CPU-bound test). Likewise if you use all the physical memory on testing something that is memory-bound then it could invalidate your results.

所以,总的原则是,该机应该是下一个最低至正常负荷进行这样的测试时。其他比没有一个多问题。是的,程序可以交换内核上运行一段时间,但这样做的开销或者是你测量的时间仅占很小比例或测量的时间是如此之小,系统的时间测量的粒度是一个问题。

So the general principle is that the machine should be under a minimal-to-normal load when conducting such tests. Other than that there isn't a multiprocessing issue. Yes, the program could swap cores while running but the overhead of doing that is either a tiny percentage of your measured time or the measured time is so small that the granularity of the system's time measurement is an issue.

这篇关于用于多处理器机器秒表最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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