EDIT: I found one speedup myself (see my answer below) but still interested in replies…
I have a critical loop which looks like this:
for j in 1:about_1000
x = a_simple_calculation()
@printf(op, " %g", x) # this is line 241
end
@printf(op, "\n")
All in all this loop runs about 12 million times, and takes about 27 seconds total, i.e. about 2 microseconds per loop. The profiler confirms that the slowest part is the @printf line inside the loop, not the calculation of x. The profiler’s output for this part was:
10007 rfdata_core.jl:241; #rfdata_core#7(::Int64, ::Function, ::Array{Float64,1}, ::Bool, ::Array{Int64,1}, ::Array{Int64,1}, ::Int64, ::Int64, ::Int64, ::...
[many lines later...]
6408 ./tuple.jl:60; indexed_iterate(::Tuple{Int32,Int32,Bool}, ::Int64, ::Int64)
3 ./int.jl:53; +
2105 ./tuple.jl:60; indexed_iterate
3 ./int.jl:53; +
I also tried building a string (basically replacing the first @printf with the appropriate @sprintf command, and writing a line at a time), and that was actually slightly slower. So that suggests the problem is not the print buffering itself.
So… is there a fastest way to print (or make strings) in Julia? Or some other obvious improvement to this code?