The printf() family of functions is one of the rare parts of the C standard library that is essentially implementing an interpreter. The format string is a program and the library call runs that program with data that is supplied in the other arguments. So in essence this is comparing an interpreter (sprintf) with a JIT compiler (PyPy generating code for a specific format string at runtime).
I agree it's not a very fair comparison: if this C code was performance critical you would call a specialized function for converting an integer to a string, and this would almost certainly beat PyPy.
And the fact that it only runs twice as long is pretty impressive, actually. Sprintf is doing the parsing 999,999,999 times more than the PyPy interpreter is.
I'm not a GCC expert, but Microsoft C compilers could inline cross-module code since the beginning of time. His example may actually produce different results under msvc with /GL flag and with static linking enabled.
"...Inline a function in a module even when the function is defined in another module..." http://msdn.microsoft.com/en-us/library/0zza0de8%28v=vs.80%2...
Edit: Is this not the case? I am under the impression that it is, and is relatively new at that.
Not being a part of the C language itself is irrelevant. The traditional linker has been a part of the C ecosystem for decades.
Dynamic linking has been around since the 1960s, and is the model that has been used by almost all currently active developers for their entire careers. Arguments based around specialized microcontrollers, hard real-time systems, life-or-death control systems, etc. are off in the woods. Few developers use such systems in the real world, and their code is statistically irrelevant to comparisons between e.g. Python and C.
Consider yourself lucky...
Provided program, -O4: ~5.06s
Provided program, -O4 -march=native: ~5.07s (i.e. no difference)
Provided program, includes sprintf and the vprintf family, -O4 -march=native: ~4.84s
Of course, this is just a silly example (realistic programs don't make things that easy on the JIT). Still, quite impressive; congratulations to the PyPy guys!
yeah, it's almost like those "$something in only $few lines of $programming_language" posts where you get highly obfuscated code to read.
The examples aren't comparable. The equivalent would be to have Python code invoke an external function which sits in a pre-compiled .so
Bulk of the work is happening inside of sprintf(), why handicap C by not letting it to compile the code?
The fair comparison would be to place the source of sprinf() nearby and see if C compiler inlines that call or/and unrolls the loop, otherwise it's just about packaging/linking, not really about code generation.
Edit: I see this became #1 on HN front page today. I want to take advantage of this and say that http://mailgun.net, the programmable email platform, is looking for an engineer who'd find this discussion interesting. See my profile. And we're users of PyPy too! :)