node.js 什么算作 CPU 密集型任务(例如排序、搜索等?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15341551/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:48:28  来源:igfitidea点击:

What counts as CPU Intensive tasks (eg. sorting, searching etc?)

javascriptnode.jscpu-usage

提问by Jiew Meng

What do you count as a CPU intensive task. In terms of ... an algorithm/code for example (not so much a use case like video editing etc). Reason is it seems the main reason not to use NodeJS something I really like is mainly CPU intensive task. So what counts as that? Is it sorting, search, graph transversal, matrix multiply, for example?

什么算作 CPU 密集型任务。就......例如算法/代码而言(而不是像视频编辑等的用例)。原因是不使用 NodeJS 的主要原因似乎是我真正喜欢的主要是 CPU 密集型任务。那算什么?例如,是排序、搜索、图形横向、矩阵乘法吗?

采纳答案by Brandon

Terms like "intensive" or "expensive" are relative and it isn't always obvious what activities are CPU-intensive. Generally speaking, anything that isn't I/O is CPU. And I/O is asynchronous in node.js, so not a problem. Therefore, we are left with everything except for I/O being expensive.

“密集型”或“昂贵”等术语是相对的,哪些活动是 CPU 密集型的并不总是显而易见的。一般来说,任何不是 I/O 的东西都是 CPU。而且 node.js 中的 I/O 是异步的,所以不是问题。因此,除了 I/O 昂贵之外,我们剩下的就是一切。

Your approach to pick general patterns is wise. Sorting, searching, and even algorithms in general are CPU-bound. Of course, you can't eliminate CPU usage, but if you can make your database sort instead of your app code, you may be better off.

您选择一般模式的方法是明智的。排序、搜索甚至算法一般都受 CPU 限制。当然,您无法消除 CPU 使用率,但如果您可以让您的数据库排序而不是您的应用程序代码,那么您可能会更好。

I would also keep an eye out for large loops. A loop that doesn't fire any asynchronous events is a bottleneck. Of course, one cannot entirely avoid loops. They are a fact of life for programming. If your loops are short, then no problem. If you find a loop that runs 10,000 times, you may want to consider breaking it up using setTimeout, process.nextTick, or a separate node process.

我也会留意大循环。不触发任何异步事件的循环是一个瓶颈。当然,不能完全避免循环。它们是编程生活中的事实。如果你的循环很短,那么没问题。如果您发现一个循环运行了 10,000 次,您可能需要考虑使用 setTimeout、process.nextTick 或单独的节点进程将其分解。

10,000 was picked arbitrarily. It depends on what the loop does. Your milage may vary.

10,000 是任意挑选的。这取决于循环的作用。您的里程可能会有所不同。

回答by user568109

Processes or tasks which run on a computer require various resources like CPU cycles, memory, disk or network which are managed by the operating system, so that each task executes efficiently (without waiting for a resource if possible).

在计算机上运行的进程或任务需要由操作系统管理的各种资源,如 CPU 周期、内存、磁盘或网络,以便每个任务高效执行(如果可能,无需等待资源)。

OS tries to maximize resource utilization by letting many processes use resources simultaneously. If a process requests a particular resource in large amount, it can bottleneck (delay) its execution. The process is said to be resource-intensive w.r.to that resource. So resource-intensive is a relative terminology.

操作系统试图通过让多个进程同时使用资源来最大化资源利用率。如果一个进程大量请求特定资源,它可能会瓶颈(延迟)其执行。据说该过程对于该资源来说是资源密集型的。所以资源密集型是一个相对的术语。

Sorting, search, graph traversal, matrix multiply are all CPU operations, a process is CPU-intensiveor not it depends on how much and how frequent are their execution. For instance trans-coding videoor compressing filesis pretty CPU intensive, because they run the CPU operations far more than they need to read/write memory or disk. If you are planing on doing these, you should create a separate child process for it, so that it won't slowdown node process, which is single-threaded, or better create a node cluster.

排序、搜索、图遍历、矩阵乘法都是 CPU 操作,进程是CPU-intensive不是取决于它们执行的次数和频率。例如,trans-coding video或者compressing files是相当占用 CPU 的,因为它们运行的​​ CPU 操作远远超出了读/写内存或磁盘的需要。如果你打算做这些,你应该为它创建一个单独的子进程,这样它就不会减慢单线程的节点进程,或者更好地创建一个节点集群

回答by onTheInternet

Bash scripting really gets into this. My professor is always harping on us about writing efficient code that will ease work on CPU

Bash 脚本确实涉及到这一点。我的教授总是叮嘱我们要编写高效的代码以减轻 CPU 的工作量

Here is a good example of inefficient practices in Linux

这是 Linux 中低效实践的一个很好的例子

http://hacktux.com/bash/script/efficient

http://hacktux.com/bash/script/efficient

Another example I can think of is recursive functions, or functions that continually call themselves until a condition is satisfied. These typically take up a lot of CPU power.

我能想到的另一个例子是递归函数,或不断调用自身直到满足条件的函数。这些通常会占用大量 CPU 资源。