System.out.err & System.out interleaving in Java -
to work out how time taken perform algorithm, in main method, not print time gets interleaved system.print.
long starttime = system.currenttimemillis(); a1.print(2); long endtime = system.currenttimemillis(); system.err.print(endtime - starttime);
and if class this:
public class a{ public void print(int n){ for(int = 0; <=n; i++){ system.out.println(i) }}
it prints
0
1
2
and in line amount of time supposed go through loop, won't, won't print this:
0
1
2
1
here last line or 1 millisecond taken algorithm. textbook says must use system.err. , figure out way prevent interleaving.
you like
system.seterr(system.out);
so output in same stream. use 2 different streams that's why interleaving.
for code be:
long starttime = system.currenttimemillis(); system.seterr(system.out); a1.print(50); long endtime = system.currenttimemillis(); system.err.print(endtime - starttime);
Comments
Post a Comment