术语“CPU限制”和“I/O限制”是什么意思?
当前回答
这很直观:
如果一个程序在CPU更快的情况下运行得更快,那么它就受到了CPU的限制,也就是说,它的大部分时间都在简单地使用CPU(进行计算)。计算π的新数字的程序通常是cpu限制的,它只是处理数字。
如果一个程序能够在I/O子系统更快的情况下运行得更快,那么它就是I/O约束的。具体的I/O系统是不同的;我通常把它与磁盘联系在一起,当然,一般来说,网络或通信也很常见。在一个大文件中查找一些数据的程序可能会成为I/O限制,因为瓶颈是从磁盘读取数据(实际上,这个例子在今天可能有点过时,从ssd读取数百MB/s)。
其他回答
看看微软怎么说。
The core of async programming is the Task and Task objects, which model asynchronous operations. They are supported by the async and await keywords. The model is fairly simple in most cases: For I/O-bound code, you await an operation which returns a Task or Task inside of an async method. For CPU-bound code, you await an operation which is started on a background thread with the Task.Run method. The await keyword is where the magic happens. It yields control to the caller of the method that performed await, and it ultimately allows a UI to be responsive or a service to be elastic.
I/ o绑定示例:从web服务下载数据
private readonly HttpClient _httpClient = new HttpClient();
downloadButton.Clicked += async (o, e) =>
{
// This line will yield control to the UI as the request
// from the web service is happening.
//
// The UI thread is now free to perform other work.
var stringData = await _httpClient.GetStringAsync(URL);
DoSomethingWithData(stringData);
};
cpu受限示例:为游戏执行计算
private DamageResult CalculateDamageDone()
{
// Code omitted:
//
// Does an expensive calculation and returns
// the result of that calculation.
}
calculateButton.Clicked += async (o, e) =>
{
// This line will yield control to the UI while CalculateDamageDone()
// performs its work. The UI thread is free to perform other work.
var damageResult = await Task.Run(() => CalculateDamageDone());
DisplayDamage(damageResult);
};
Examples above showed how you can use async and await for I/O-bound and CPU-bound work. It's key that you can identify when a job you need to do is I/O-bound or CPU-bound, because it can greatly affect the performance of your code and could potentially lead to misusing certain constructs. Here are two questions you should ask before you write any code: Will your code be "waiting" for something, such as data from a database? If your answer is "yes", then your work is I/O-bound. Will your code be performing a very expensive computation? If you answered "yes", then your work is CPU-bound. If the work you have is I/O-bound, use async and await without Task.Run. You should not use the Task Parallel Library. The reason for this is outlined in the Async in Depth article. If the work you have is CPU-bound and you care about responsiveness, use async and await but spawn the work off on another thread with Task.Run. If the work is appropriate for concurrency and parallelism, you should also consider using the Task Parallel Library.
IO绑定进程:花更多的时间做IO比计算,有很多 短CPU突发。 CPU绑定进程:花费更多的时间进行计算,很少有很长时间的CPU爆发
这很直观:
如果一个程序在CPU更快的情况下运行得更快,那么它就受到了CPU的限制,也就是说,它的大部分时间都在简单地使用CPU(进行计算)。计算π的新数字的程序通常是cpu限制的,它只是处理数字。
如果一个程序能够在I/O子系统更快的情况下运行得更快,那么它就是I/O约束的。具体的I/O系统是不同的;我通常把它与磁盘联系在一起,当然,一般来说,网络或通信也很常见。在一个大文件中查找一些数据的程序可能会成为I/O限制,因为瓶颈是从磁盘读取数据(实际上,这个例子在今天可能有点过时,从ssd读取数百MB/s)。
CPU限制是指程序被CPU或中央处理器所限制,而I/O限制是指程序被I/O或输入/输出所限制,例如读写磁盘、网络等。
一般来说,在优化计算机程序时,人们试图找出瓶颈并消除它。知道您的程序受CPU限制是有帮助的,这样就不会不必要地优化其他东西。
[我所说的“瓶颈”是指使你的程序运行得比原本要慢的东西。]
I/O绑定进程:—如果一个进程生命周期的大部分时间都处于I/O状态,那么这个进程就是一个I/O绑定进程。例子:计算器,internet explorer
CPU绑定进程:—如果进程生命周期的大部分时间都花在CPU上,那么它就是CPU绑定进程。