简体   繁体   中英

Does console write speed affect speed of program execution

Does having console.log on every iteration of a huge for loop (lets say i<100 000) affect the overall speed of execution?

In other words would the iteration occur faster if there was no console.log ?

Im not asking if there would be "ANY" difference, but is there a noticable difference (like 10ms instead of 1ms)?

In my case I'm talking about windows command prompt and javascript

Yes, console writing does affect program execution time.

In fact it takes a considerable period of time to write to a console. Depending on what the console actually is the time will vary too. So, a text console on a display local to the running computer will be a lot faster than a console at the other end of an ssh link across a network. I imagine that a console viewed as part of a Remote Desktop Session won't be any faster or slower than a local one - RDP sends the entire framebuffer periodically, so it won't be trying to keep up with every last console print.

Some runtimes do interesting things. I know that your question was tagged Javascript, but bear with me. The printf() funtcion as implemented in gcc on Linux prints to stdout, and that is itself buffered by the runtime library through IPC pipes so that if you have multiple threads all calling printf() at the same time then the output isn't too badly jumbled up. That buffering through pipes actually buys the application some time back; the pipe(s) can absorb the text output quickly so that the runtime can get on with the slow business of writing the text to screen. I don't know if Win32 (the ultimate basis of everything in Windows) does a similar thing.

So if a Javascript runtime is built on top of C/C++ compiled on gcc on Linux there is all those layers to get through in addition to whatever layers the Javascript runtime adds itself.

In C/C++ GCC Linux stderr isn't buffered in this way, it just goes direct to the screen as soon as possible. This can lead to some really confusing situations, like

"How did it print that error before that printf()? They're the other way round in my source code!!!!"

Any runtime like Javascript built on top of C/C++ will also inherit that behaviour!

It also seems to be the cause of recent Eclipse's getting compiler output order wrong in its compile console for CDT. I think that Eclipse changed the way in which it grabbed console output from programmes like gcc without realising that gcc, which is in itself compiled with gcc, would not necessarily be putting out stderr and stdout messages in strict 'time' order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM