C#中的CPU性能 [英] CPU performance in C#

查看:109
本文介绍了C#中的CPU性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我正在尝试编写一个自动创建计算机可以处理的线程的程序(不是恶意软件!)。



如何创建一个PerformanceCounter实例来获取所有CPU /内核的使用?

我所见过的所有实例都是个人核心。





非常感谢!

Hi all!

I'm trying to write a program that automatically creates as many threads as the computer can handle (not malware!).

How do you create an instance of PerformanceCounter that gets the usage across all CPUs/Cores?
All I've seen are examples that work on an individual core.


Thank you very much!

推荐答案

In一般来说,在一个极限的情况下驾驶系统是一个坏主意。

但是每个核心计数器有什么问题?汇总它们,您就会得到一个概述。

我建议你在这里查看两种方法: http://allen-conway-dotnet.blogspot.hu/2013/07/get-cpu-usage-across-all-cores-in-c.html [<一个href =http://allen-conway-dotnet.blogspot.hu/2013/07/get-cpu-usage-across-all-cores-in-c.html\"target =_ blanktitle =新窗口 > ^ ]



不过,我会考虑将至少一个核心保留在100%以下,因为你的系统会变得没有响应,你就没有实际的意味着控制它。更重要的是,我会设置亲和力 [ ^ 这些线程省略一个特定的核心(0)。
In general it is a bad idea to drive a system at it's limits.
But what's the problem with having those per core counters? Aggregate them, and you get an overview.
I suggest you check both approaches here: http://allen-conway-dotnet.blogspot.hu/2013/07/get-cpu-usage-across-all-cores-in-c.html[^]

Still, I would consider leaving at least one core below 100%, since your system would become unresponsive, and you would have no actual means to control it. Even more, I would set the affinity[^] of those threads to omit one specific core (0).


如果我对问题的评论仍然不清楚:只要你有更多的线程,那么CPU核心(通常比系统可能处理的线程数少得多),添加每个线程只会降低性能。



线程主要用于实现你需要逻辑上独立和几乎独立的计算场景的场景,而不是为了提高性能。







如果您确实需要测量一些吞吐量而不是性能计数,请使用类 System.Diagnostic s.Stopwatch

http ://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx [ ^ ]。



这个东西的准确性非常高。要想一想,请阅读静态属性 IsHighResolution (它应该在Windows上返回true)和频率

http://msdn.microsoft.com/en-us /library/system.diagnostics.stopwatch.ishighresolution.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency.aspx [ ^ ]。



这是你能得到的最准确的方法。



-SA
In case my comments to the question are still unclear: as soon as you have more threads then the CPU cores (which is usually much, much less then the number of threads the system can possibly handle), adding each thread only degrades performance.

Threads are used mostly to implement scenarios where you need logically separate and almost independent scenarios of computing, not for improving performance.



If you really need to measure some throughput, instead of performance counting, use the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

The accuracy of this thing is very high. To get an idea, read the static properties IsHighResolution (it should return true on Windows) and Frequency:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency.aspx[^].

This is the most accurate method you can get.

—SA


1 。要使用多个内核,您必须将计算分解为多个线程。操作系统无法为您执行此操作。许多问题都无法解决,所以你需要进行单线程,单核计算。



2.如果你能够重写你的计算以便它可以被打破在多个并行操作中,可以为这些操作分配单独的线程。请参阅OpenMP或并行。



http:/ /www.greatlakesconsortium.org/events/scaling/files/openmp09.pdf [ ^ ]



OpenMP风格多线程 [ ^ ]



3.操作系统将在多个上共享这些线程核心。



4.即便如此,由于开销不是所有问题都会看到计算速度的提高。有些人实际上可能会放慢速度。
1. To use multiple cores you have to break your computation into multiple threads. The OS can't do that for you. Many problems cannot be broken down so you are left with single threaded, single core computation.

2. If you are able to rewrite your computation so that it can be broken into multiple parallel operations these can then be allocated separate threads. See OpenMP or Parallel.

http://www.greatlakesconsortium.org/events/scaling/files/openmp09.pdf[^]

OpenMP style multi-threading in C#[^]

3. The OS will then share these threads over multiple cores.

4. Even then because of overheads not all problems will see a gain in computational speed. Some may actually slow down.


这篇关于C#中的CPU性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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