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

十年热恋 提交于 2019-12-18 11:49:10

问题


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?


回答1:


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.

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.

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 was picked arbitrarily. It depends on what the loop does. Your milage may vary.




回答2:


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).

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-intensive or not it depends on how much and how frequent are their execution. For instance trans-coding video or compressing files is 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.




回答3:


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

Here is a good example of inefficient practices in Linux

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.



来源:https://stackoverflow.com/questions/15341551/what-counts-as-cpu-intensive-tasks-eg-sorting-searching-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!