什么算作CPU密集型任务(例如排序,搜索等?) [英] What counts as CPU Intensive tasks (eg. sorting, searching etc?)

查看:199
本文介绍了什么算作CPU密集型任务(例如排序,搜索等?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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密集型的。一般来说,任何非I / O都是CPU。 I / O在node.js中是异步的,所以不是问题。因此,除了I / O价格昂贵之外,我们只剩下一切。

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限制的。当然,你不能消除CPU使用率,但是如果你可以使你的数据库排序而不是你的应用程序代码,你可能会更好。

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.

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

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是任意选择的。这取决于循环的作用。你的milage可能会有所不同。

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

这篇关于什么算作CPU密集型任务(例如排序,搜索等?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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