function printfd(n)f = open("/dev/null","w")for i = 1:n# printf(f, f"%d %d\n", i, i)_jl_int_dec(i)_jl_int_dec(i)write(f, pointer(_jl_digits), _jl_length[1])endclose(f)end
from __future__ import print_functionimport timedef test(n):# code to be benchmarkedtmin = float('inf')for i in xrange(5):t = time.time()l = test(100000)t = time.time()-tif t < tmin: tmin = tprint("Time: " + str(tmin*1000) + " ms")
def test(n):for i in xrange(n):"%d %d" % (i, i)
Time: 10.8501911163 ms.
def test(n):for i in xrange(n):"%d" % (i)Time: 9.82618331909 ms
def test(n):# f = open("/dev/null","w")for i in xrange(n):"%d %d" % (i, 3*i)
Time: 20.9879875183 ms
def test(n):f = open("/dev/null","w")for i in xrange(n):print("%d %d\n" % (i, i), file=f)Time: 181.813955307 ms
Looking at all the printf code, it turns out most of the work (for
both Stefan and the CPU) is decoding numbers and manipulating digits
(and doing I/O). Especially since our %d and %f both handle all kinds
of numbers. I didn't realize at first how much work needed to be done
dynamically on the generated digits.
The parsing portion of it is only about 100 lines. Indeed, maybe the
code generation is not worth it. I also worry that people might build
format strings at run time, in which case our performance would be
truly awful. And then there's the calling syntax issue.
It's worth doing the experiment of repeating the "%d" benchmark with
parsing done at run time, and see how much difference it makes.
Very interesting. Good detective work. A fine example of playing
benchmark games. So it turns out my I/O code is not so horrible and
slow after all :)
Looking at all the printf code, it turns out most of the work (for both Stefan and the CPU) is decoding numbers and manipulating digits (and doing I/O). Especially since our %d and %f both handle all kinds of numbers. I didn't realize at first how much work needed to be done dynamically on the generated digits. The parsing portion of it is only about 100 lines.
Indeed, maybe the code generation is not worth it. I also worry that people might build format strings at run time, in which case our performance would be truly awful. And then there's the calling syntax issue.
for i = 1:100000printf("%d %d\n", i, i)end
fmt = f"%$(n)d %$(n)d\n"for i = 1:100000fmt(i,i)end
It's worth doing the experiment of repeating the "%d" benchmark with
parsing done at run time, and see how much difference it makes.
fmt = "%$(n)d"
for i=1:k
printf(fmt, i)
end