printf too slow

288 views
Skip to first unread message

Ian Watson

unread,
Jun 15, 2012, 10:25:18 PM6/15/12
to juli...@googlegroups.com
I was pleased to find that Julia can do formatted output via the printf function. But I find that the performance of the current implementation makes it unusable.

Consider the programme

for i = 1:10000
  x = randn()
  println("$(x)")
# printf("%.2f\n", x)
end


Using println, this takes around 2 seconds (including Julia startup time). Using printf, the execution time goes to 102 seconds (including Julia startup time). I was scratching my head trying to figure out why Julia was running more than100x slower than my C++ code. Having short circuited all the computation, I was surprised to find that it was the programme's final I/O that was the killer.

The current implementation looks very elegant, I cannot say I understand it, but there does appear to be some significant performance problems with it. Assuming of course that I am using it properly...

Ian

Stefan Karpinski

unread,
Jun 15, 2012, 10:57:02 PM6/15/12
to juli...@googlegroups.com
Yeah, the better way to write this is like this:

for i = 1:10000
  x = randn()
  printf(f"%.2f\n", x)
end

I'm going to change this soon so that it's written just using print:

for i = 1:10000
  x = randn()
  print(f"%.2f\n", x)
end

It's a bit weird and punny, but after a whole lot of discussion and thought, this seems to be the best way to do this.

Ian Watson

unread,
Jun 15, 2012, 11:42:29 PM6/15/12
to juli...@googlegroups.com
Works great, and makes sense -feels like the r"xxx" thing for regexps. The run times are now indistinguishable. Would be good to get this documented, since I imagine it will be a common pit-fall. I do seem to be stepping into a lot of pot holes...

Thanks as always..

Ian

Patrick O'Leary

unread,
Jun 15, 2012, 11:43:43 PM6/15/12
to juli...@googlegroups.com
Without the format string literal, is it reinterpreting the format string on every call, then?

Stefan Karpinski

unread,
Jun 15, 2012, 11:46:40 PM6/15/12
to juli...@googlegroups.com
Yup. It's doing code gen every iteration of the loop, which is obviously horrifyingly slow. The idea behind overloading print is that if *forces* using the f"" form since otherwise the first argument is obviously just a string. The f"" form expands the format string into code at compile time, which is fast. If you want to use a dynamically generated format string, don't.

 ;-)
Reply all
Reply to author
Forward
0 new messages