You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I like to use BufferedStreams for printing to stdout because it's much faster. However, if I forget to flush the buffer, then not all the result get printed to stdout.
using Random, BufferedStreams
let ss = [randstring(20) for _ in1:10^5]
@timeforeach(println, ss) # 5.463939 seconds (708.34 k allocations: 16.149 MiB)end# Incomplete resultlet ss = [randstring(20) for _ in1:10^5]
io =BufferedOutputStream(stdout)
@timeforeach(ss) do s
println(io, s)
end# 0.554888 seconds (2.63 k allocations: 180.951 KiB, 2.34% compilation time)# BUT it's incompleteend;
let ss = [randstring(20) for _ in1:10^5]
io =BufferedOutputStream(stdout)
@timebeginforeach(ss) do s
println(io, s)
endflush(io)
end# 0.547968 seconds (2.64 k allocations: 181.054 KiB, 2.37% compilation time)end;
What is a good solution to forgetting to flush the buffer?
One approach would be a do block that introduces the buffer at the beginning and flushes it at the end.
functionbuffering(f, out)
io =BufferedOutputStream(out)
tryf(io)
finallyflush(io)
endendlet ss = [randstring(20) for _ in1:10^5]
@timebuffering(stdout) do io
foreach(ss) do s
println(io, s)
endend# 0.586527 seconds (8.66 k allocations: 712.136 KiB, 3.35% compilation time)end;
The text was updated successfully, but these errors were encountered:
I like to use BufferedStreams for printing to stdout because it's much faster. However, if I forget to
flush
the buffer, then not all the result get printed tostdout
.What is a good solution to forgetting to flush the buffer?
One approach would be a
do
block that introduces the buffer at the beginning and flushes it at the end.The text was updated successfully, but these errors were encountered: