为什么异步编程更快 [英] Why is async programming faster

查看:73
本文介绍了为什么异步编程更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断听到使用异步编程模式将使我的代码运行得更快.为什么会这样呢?不管是现在运行还是以后运行,都不必以相同的方式运行相同的代码吗?

I keep hearing that using async programming patterns will make my code run faster. Why is that true? Doesn't the same exact code have to run either way, whether it runs now or it runs later?

推荐答案

它并不快,只是不浪费时间.

It's not faster, it just doesn't waste time.

等待I/O时,同步代码停止处理.这意味着在读取文件时,您将无法运行任何其他代码.现在,如果在读取该文件时您没有其他事情要做,那么异步代码不会给您带来太多好处.

Synchronous code stops processing when waiting for I/O. Which means that when you're reading a file you can't run any other code. Now, if you have nothing else to do while that file is being read then asynchronous code would not buy you anything much.

通常,可以使用的额外CPU时间对于服务器很有用.所以问题是为什么异步编程而不是为每个客户端启动新线程?

Usually the additional CPU time that you can use is useful for servers. So the question is why do asynchronous programming instead of starting up a new thread for each client?

事实证明,启动和拆除线程很昂贵.早在2000年代早期,一个Web服务器基准测试发现tclhttpd在提供静态图像文件方面优于Apache.尽管事实是tclhttpd是用tcl编写的,而Apache是​​用C编写的,并且已知tcl的速度是C的50倍.由于tcl具有易于使用的异步I/O API,因此Tcl设法对付Apache.所以tclhttpd使用了它.

It turns out that starting and tearing down threads is expensive. Some time back in the early 2000s a web server benchmark found that tclhttpd compared favorably to Apache for serving static image files. This is despite the fact that tclhttpd was written in tcl and Apache was written in C and tcl was known to be 50 times slower than C. Tcl managed to hold its own against Apache because tcl had an easy to use asynchronous I/O API. So tclhttpd used it.

不是C没有异步I/O API.只是它们很少使用.因此,Apache没有使用它.如今,Apache2在内部与线程池一起使用异步I/O. C代码最终看起来更复杂,但是速度更快-经验教训.

It's not that C doesn't have asynchronous I/O API. It's just that they're rarely used. So Apache didn't use it. These days, Apache2 uses asynchronous I/O internally along with thread pools. The C code ends up looking more complicated but it's faster - lesson learned.

这导致我们最近对异步编程产生了痴迷.人们为什么迷恋它? (例如,有关JavaScript编程的Stackoverflow上的大多数答案都建议您永远不要使用异步功能的同步版本.)

Which leads us to the recent obsession with asynchronous programming. Why are people obsessed with it? (most answers on Stackoverflow about javascript programming for example insist that you should never use synchronous versions of asynchronous functions).

这可以回溯到您很少在C语言中看到异步程序,尽管这是一种更好的处理方式(GUI代码是一个例外,因为UI库很早就学会了依赖异步编程和事件). C语言中太多同步的功能.因此,即使您想进行异步编程,您迟早也会最终调用同步函数.另一种选择是放弃stdlib并为所有内容编写自己的异步库-从文件I/O到网络再到SQL.

This goes back to how you rarely see asynchronous programs in C even though it's the superior way of doing things (GUI code is an exception because UI libraries learned early on to rely on asynchronous programming and Events). There are simply too many functions in C that are synchronous. So even if you wanted to do asynchronous programming you'll end up calling a synchronous function sooner or later. The alternative is to abandon stdlib and write your own asynchronous libraries for everything - from file I/O to networking to SQL.

因此,在像javascript这样的语言中,异步编程最终成为默认样式,其他程序员迫切要求不要弄乱它,并意外地引入同步函数,这些函数很难与异步代码集成,而不会损失很多性能.因此,最终,异步代码像税收一样,已经成为一种社会契约.

So, in languages like javascript where asynchronous programming ended up as the default style there is pressure from other programmers to not mess it up and accidentally introduce synchronous functions which would be hard to integrate with asynchronous code without losing a lot of performance. So in the end, like taxes, asynchronous code has become a social contract.

这篇关于为什么异步编程更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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