Kitchi
Kitchi

Reputation: 1904

I/O to Screen/Standard Output

I noticed that in any program that I write, the time taken to execute is significantly slower if I do a lot of printing to the monitor (i.e. printf or fprintf (stderr, "...")).

I figured that writing to the disk will be slow, because of the physical limitations of the disk. I'm not sure why printing to the screen should slow the program down significantly.

Upvotes: 3

Views: 129

Answers (3)

Mats Petersson
Mats Petersson

Reputation: 129454

Depending on the type of screen, the size of it, the graphics card or whatever the output goes through, yes, printing stuff can add significant time to the execution time. It is very likely that printing to disk is actually faster - disks can take several megabytes per second (theoretically around 300-600MB/s on a modern SATA drive, but in reality, sooner or later it will be slower than that, as the disk actually needs to move the data onto the platters inside the disk - but they also have large caches, so writing 16MB or 32MB can take quite some time). Try adding some timestamps to your code (if you haven't already) and compare myprog with myprog > file and myprog > /dev/null (or NUL: if you are windows), and see which one takes more time - I bet it's the one going to the screen - and writing to the null device is the fastest, but only a little bit.

The biggest problem with printing to screen is the scrolling - it means "shuffle everythng", and even with clever hardware, it can be quite a lot of pixels to shuffle around. Bear in mind that modern graphics cards are much more oriented towards drawing 3D, which is done in quite a different way from scrolling 2D text screens.

And of course, on top of that, your application itself takes more time because you are calling printf or similar functions, which aren't entirely trivial functions, so if you print a lot of stuff, printf in itself takes some time - for programs with simple code, this can be 10x the time it takes to do the actual work, even if the data goes to "null".

Upvotes: 3

fuz
fuz

Reputation: 93082

Writing to a terminal is not exactly fast and is not designed to be fast since usually the only stuff that goes to stdout are pieces of information for the user who is bound by how fast he or she can read stuff.

When you write to the terminal on eg. linux, the following stuff happens (maybe). The kernel is being called between each two consecutive steps:

  1. Your program issues a write system call to write data.
  2. The kernel resumes your terminal emulator and yields the data written to the terminal device
  3. The terminal emulator renders its frame's new state and sends this to the X server
  4. The X server manipulates memory mapped display regions
  5. The kernel tells your graphics card to swap buffers

And those are a lot of steps... Since terminal output is usually only line-buffered, this occurs pretty often.

Upvotes: 3

P.P
P.P

Reputation: 121407

printf() is usually line-buffered when printing to the screen (terminal) which makes the output slower.

You can set the buffer size for stdout using setvbuf().

Upvotes: 2

Related Questions