在 JavaScript 中获取 CPU 核心数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3289465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get number of CPU cores in JavaScript?
提问by tsauerwein
Is there a way to determine the number of available CPU cores in JavaScript, so that you could adjust the number of web workers depending on that?
有没有办法确定 JavaScript 中可用 CPU 内核的数量,以便您可以根据此调整 Web Worker 的数量?
回答by Eli Grey
Yes. To quote MDN:
是的。引用 MDN:
The
navigator.hardwareConcurrencyread-only property returns the number of logical processors available to run threads on the user's computer…Modern computers have multiple physical processor cores in their CPU (two or four cores is typical), but each physical core is also usually able to run more than one thread at a time using advanced scheduling techniques. So a four-core CPU may offer eight logical processor cores, for example. The number of logical processor cores can be used to measure the number of threads which can effectively be run at once without them having to context switch.
The browser may, however, choose to report a lower number of logical cores in order to represent more accurately the number of Workers that can run at once, so don't treat this as an absolute measurement of the number of cores in the user's system.
该
navigator.hardwareConcurrency只读属性返回逻辑处理器可以运行在用户的计算机上的线程数...现代计算机的 CPU 中有多个物理处理器内核(典型的是两个或四个内核),但每个物理内核通常也能够使用高级调度技术一次运行多个线程。例如,四核 CPU 可以提供八个逻辑处理器内核。逻辑处理器内核的数量可用于衡量可以一次有效运行而不必进行上下文切换的线程数量。
但是,浏览器可能会选择报告较少的逻辑内核数,以便更准确地表示可以同时运行的 Worker 数,因此不要将此视为用户系统中内核数的绝对测量值.
It's supported by every browser except Internet Explorer.For that, you can use the polyfill core-estimator(demo, blog post).
除了 Internet Explorer 之外,所有浏览器都支持它。为此,您可以使用 polyfill core-estimator(演示、博客文章)。
回答by Darin Dimitrov
No, there isn't, unless you use some ActiveX.
不,没有,除非您使用某些 ActiveX。
回答by rashtao
You can try to estimate the number of cores with: https://github.com/oftn/core-estimator
您可以尝试使用以下方法估算内核数:https: //github.com/oftn/core-estimator
demo: http://eligrey.com/blog/post/cpu-core-estimation-with-javascript
演示:http: //eligrey.com/blog/post/cpu-core-estimation-with-javascript
回答by dlongley
Here's a fairly quick concurrency estimator I hacked together... it hasn't undergone much testing yet:
这是我一起开发的一个相当快速的并发估计器......它还没有经过太多测试:
Here's the code the workers run (since I have a jsfiddle link a sample is necessary):
这是工作人员运行的代码(因为我有一个 jsfiddle 链接,所以需要一个示例):
// create worker concurrency estimation code as blob
var blobUrl = URL.createObjectURL(new Blob(['(',
function() {
self.addEventListener('message', function(e) {
// run worker for 4 ms
var st = Date.now();
var et = st + 4;
while(Date.now() < et);
self.postMessage({st: st, et: et});
});
}.toString(),
')()'], {type: 'application/javascript'}));
The estimator has a large number of workers run for a short period of time (4ms) and report back the times that they ran (unfortunately, performance.now() is unavailable in Web Workers for more accurate timing). The main thread then checks to see the maximum number of workers that were running during the same time. This test is repeated a number of times to get a decent sample to produce an estimate with.
估计器有大量工作人员在短时间内(4ms)运行并报告他们运行的时间(不幸的是,性能.now() 在 Web Workers 中不可用以获得更准确的计时)。主线程然后检查以查看在同一时间运行的最大工作线程数。该测试重复多次,以获得一个不错的样本来产生估计。
So the main idea is that, given a small enough chunk of work, workers should only be scheduled to run at the same time if there are enough cores to support that behavior. It's obviously just an estimate, but so far it's been reasonably accurate for a few machines I've tested -- which is good enough for my use case. The number of samples can be increased to get a more accurate approximation; I just use 10 because it's quick and I don't want to waste time estimating versus just getting the work done.
因此,主要思想是,如果工作量足够小,则只有在有足够的内核支持该行为的情况下,才应安排工作人员同时运行。这显然只是一个估计,但到目前为止,对于我测试过的几台机器来说,它是相当准确的——这对我的用例来说已经足够了。可以增加样本数量以获得更准确的近似值;我只使用 10,因为它很快,而且我不想浪费时间进行估算而不是完成工作。
回答by Dario
If you are going to do data crunching on your workers, navigator.hardwareConcurrencymight not be good enough as it returns the number of logical cores in modern browsers, you could try to estimate the number of physical cores using WebCPU:
如果您打算对工作人员进行数据处理,navigator.hardwareConcurrency可能还不够好,因为它返回现代浏览器中的逻辑核心数,您可以尝试使用WebCPU估算物理核心数:
import {WebCPU} from 'webcpu';
WebCPU.detectCPU().then(result => {
console.log(`Reported Cores: ${result.reportedCores}`);
console.log(`Estimated Idle Cores: ${result.estimatedIdleCores}`);
console.log(`Estimated Physical Cores: ${result.estimatedPhysicalCores}`);
});

