0

I started out just running some tests to see the speed difference between writing to a file and printing to the console, and how much of a difference there was between SSD and HDD. My program just prints the numbers 0-10,000,000

Console: 6.089 file: 4.269

I also ran this test up to a hundred million and consistently saw the same ratios of times. I also checked changing the order of the tests and saw no change in speed.

Here's where it gets weird. I changed both printlns to .println(i*i+42/7*9-89*2%400/2);

after doing this I got

Console: 8.586
file: 4.475

Where the console time increased significantly, but the file time did not. As a final oddity I changed it to .println( ( i*i+42/7*9-89*2 ) %400/2) and in this case I actually saw a speed up in console output.

Console: 4.352
file: 4.66

Can anyone explain these oddities? I can't seem to find any reason for the drastic speed changes. I'm thinking perhaps it's just a change in the number of bits that have to be written, but I cannot explain why it only effects the console's speed.

Any help or answers are very much appreciated! This problem has been bothering me for a while so I thought I would ask the experts!

linear__
  • 1
  • 1
  • The computation is almost nothing compared to the IO. If you see a difference it is more likely to be a warmup issue or the about of data you are writing. – Peter Lawrey Apr 10 '16 at 07:07
  • I considered that, but as I said I ran larger tests and saw the same sort of increase in time in the console and I also got the same times by changing the order of the two tests. – linear__ Apr 10 '16 at 14:43

1 Answers1

1

Hereby a explanation of why printing to a console is slower than to file (taken from why is system out println so slow).

println is not slow, it's the underlying PrintStream that is connected with the console, provided by the hosting operating system is slow because:

  1. The bytes have to be sent to the console application (should be quite fast)
  2. Each char has to be rendered using (usually) a true type font (that's pretty slow, switching off anti aliasing could improve performance, btw)
  3. The displayed area may have to be scrolled in order to append a new line to the visible area (best case: bit block transfer operation, worst case: re-rendering of the complete text area)

The sudden increase of speed due to the different calculations I can't really give a explanation. I was initially thinking of casting from int to double, etc but that should apply to both println's.

Is the result that is printed longer than the width of your console?

Community
  • 1
  • 1
uniknow
  • 938
  • 6
  • 5
  • 1
    The first println effectively prints some number around `i*i` while the second effectively outputs `(i*i +x) %200`. For i in 0-10,000,000 it is simply much less output. – makadev Apr 10 '16 at 05:14
  • No, all the results barely take up a quarter of the consoles width. I had some ideas why the console was slower but thank you for the explanation! I still cannot reason out why there would be such a drastic time difference for fairly minor computations, especially for only the console. – linear__ Apr 10 '16 at 14:47