C中相差了7秒,C++ 5秒,Python 10秒,为什么会这样?

C语言中, printf("number\n") 比 printf("number") 、
C++中, std::cout << number << std::endl 比 std::cout << number 、
Python 中, print number 比 print number,
都要花上更多的时间,比如从一打印到一百万,
C中相差了7秒,C++ 5秒,Python 10秒(虽然python中后一种是把换行改成空格)。
请问为什么会这样?

交互式爱情
浏览 129回答 1
1回答

慕姐4208626

默认情况下stdout是行缓冲的,没有换行的话,程序会尽量缓存,直到某个阈值(比如1024)为止。缓存减少了与IO设备的交互,所以显然速度会更快。如果你在运行的时候把输出重定向文件(文件默认是全缓冲),估计就看不出差别了。
打开App,查看更多内容
随时随地看视频慕课网APP