Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 230
Jiew Meng Avatar asked Mar 11 '13 14:03

Jiew Meng


People also ask

What is considered a CPU intensive task?

So what are CPU Intensive tasks? They are complex user actions that eat up more RAM. A few of such processes can shut down your server entirely. Naturally, you want to make sure that your app or website is 'smart' enough to handle different kinds of tasks, for each individual user request.

What is a CPU task?

The CPU performs basic arithmetic, logic, controlling, and input/output (I/O) operations specified by the instructions in the program. This contrasts with external components such as main memory and I/O circuitry, and specialized processors such as graphics processing units (GPUs).

What are CPU intensive tasks in node JS?

Node. js uses a single thread to handle many clients, and while I/O operations can run asynchronously, CPU-intensive code can not. It means that too many (or too long) CPU-intensive tasks could keep the main thread too busy to handle other requests, practically blocking it.


2 Answers

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.

like image 106
Brandon Avatar answered Oct 02 '22 15:10

Brandon


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.

like image 45
user568109 Avatar answered Oct 02 '22 15:10

user568109