In an online judge programming contest problem I am required to output up to 50,000 lines in under 1 second via standard out (in addition to reading in up to 200,000 pairs of integers which I used a buffer for). My logic seems to be correct but I keep getting my submissions turned down for exceeding the 1 second runtime. I stripped down my code logic to just output a constant string and it still exceeds the time limit.
Is there a faster way to output than using System.out.println(String s)for every line of output?
I would use a single
System.out.printcall (or the least that makes sense which is to be found out via benchmarking) like this:Edit:
sb.toString()is not a free operation.The above takes ~650ms on my notebook (500,000 instead of the requested 50,000).
Edit2: with two other tricks, in case the filling time matters:
don’t append
for every line (the code below appends 200 lines every time, for this
it uses a temp
sb1); only possible if every line can have the samecontent. Enjoy.
~500ms in my case.