News for hackers. Stuff that matters.
Re:PyPy is faster than C, again: string formatting by old-gregg on Tuesday August 02, 2011 @03:29PM (#2838395)
I love PyPy, those guys are amazing. However, every time I see "faster than C" almost always a bit of trickery/wordplay is involved.

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! :)

Re:PyPy is faster than C, again: string formatting by haberman on Tuesday August 02, 2011 @05:10PM (#2838710)
I highly doubt that the performance difference is a result of not inlining/unrolling sprintf(). The difference is most likely due to the fact that sprintf() is parsing the format string over and over, whereas PyPy is only parsing it once.

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.

Re:PyPy is faster than C, again: string formatting by cube13 on Tuesday August 02, 2011 @06:08PM (#2838908)
>I highly doubt that the performance difference is a result of not inlining/unrolling sprintf(). The difference is most likely due to the fact that sprintf() is parsing the format string over and over, whereas PyPy is only parsing it once.

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.

Re:PyPy is faster than C, again: string formatting by scott_s on Tuesday August 02, 2011 @03:53PM (#2838458)
But the whole point of the post was to point out that PyPy can optimize in places that the traditional C model of shared libraries cannot - or, at least, have great difficulty doing so. This is an inherent advantage to optimizing the instructions at runtime.
Re:PyPy is faster than C, again: string formatting by old-gregg on Tuesday August 02, 2011 @04:07PM (#2838498)
Shared libraries is an operating system feature and has nothing to do with C. C just happens to be the most popular systems programming language. "Traditional C model" is just a bunch of object files, and it's up to you and your linker to package it any way you want it: a shared library, a static library or an executable.

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...

Re:PyPy is faster than C, again: string formatting by ori_b on Tuesday August 02, 2011 @04:46PM (#2838638)
GCC got that option recently with '-flto' (or whatever it was to enable link time optimization)
Re:PyPy is faster than C, again: string formatting by sausagefeet on Tuesday August 02, 2011 @06:40PM (#2838995)
Is anyone working on a JIT for object code? I'm thinking we could keep all our SO's how they are now since they are great for updating versions of libraries with bugs, but have a JIT that takes care of profiling and inlining functions from other modules.
Re:PyPy is faster than C, again: string formatting by fanf2 on Wednesday August 03, 2011 @07:02AM (#2840599)
LLVM is designed to be able to do this.
Re:PyPy is faster than C, again: string formatting by scott_s on Tuesday August 02, 2011 @04:14PM (#2838524)
See my reply below. I'm fully aware of the relationship C has to systems programming.
Re:PyPy is faster than C, again: string formatting by cube13 on Tuesday August 02, 2011 @04:14PM (#2838526)
Depends on their GCC options. If it's the default option, it should be using the shared library. If they changed it to statically link libc, that should produce a(possibly considerable) performance boost.
Re:PyPy is faster than C, again: string formatting by burgerbrain on Tuesday August 02, 2011 @04:02PM (#2838485)
C the language doesn't have shared libraries. That's something some systems add themselves.

Edit: Is this not the case? I am under the impression that it is, and is relatively new at that.

Re:PyPy is faster than C, again: string formatting by scott_s on Tuesday August 02, 2011 @04:07PM (#2838497)
Which is why I said "the traditional C model of shared libraries." Shared libraries are not a part of the C language itself, but most real systems make heavy use of shared libraries. Even statically linked C programs have this problem, because optimizing across object files is hard - most compilers don't even try.

Not being a part of the C language itself is irrelevant. The traditional linker has been a part of the C ecosystem for decades.

Re:PyPy is faster than C, again: string formatting by burgerbrain on Tuesday August 02, 2011 @04:13PM (#2838519)
Statically linked programs certainly still have this problem, I'm not contending that at all. I would not however call shared libraries a "traditional model" at all. Particularly in the fields that C seems to be getting relegated to more and more these days.
Re:PyPy is faster than C, again: string formatting by scott_s on Tuesday August 02, 2011 @04:15PM (#2838529)
If you're running a Unix derived system, most of the programs are using shared libraries. That fits "traditional" in my mind.
Re:PyPy is faster than C, again: string formatting by burgerbrain on Tuesday August 02, 2011 @04:28PM (#2838574)
A sufficiently modern Unix derived system, sure ;) I have quite commonly seen shared libraries not done on embedded systems however, regardless of heritage.
Re:PyPy is faster than C, again: string formatting by scott_s on Tuesday August 02, 2011 @04:31PM (#2838584)
And I don't consider embedded systems to be traditional. Otherwise, we wouldn't have to qualify it with "embedded."
Re:PyPy is faster than C, again: string formatting by burgerbrain on Tuesday August 02, 2011 @04:39PM (#2838613)
They are certainly a traditional stronghold of straight up C.
Re:PyPy is faster than C, again: string formatting by nknight on Tuesday August 02, 2011 @05:13PM (#2838721)
The embedded systems I work on use shared libraries, C++, shell scripts, and even Adobe Flash.

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.

Re:PyPy is faster than C, again: string formatting by burgerbrain on Tuesday August 02, 2011 @06:32PM (#2838971)
"The embedded systems I work on use shared libraries, C++, shell scripts, and even Adobe Flash."

Consider yourself lucky...

Re:PyPy is faster than C, again: string formatting by skorgu on Tuesday August 02, 2011 @04:01PM (#2838482)
PyPy tries to move as much code as possible out of C and into (R)Python for the exact reason that this kind of optimization becomes possible/feasible/easier with JIT. So it's a packaging win sure but it's not an accidental one.
Re:PyPy is faster than C, again: string formatting by JoachimSchipper on Tuesday August 02, 2011 @04:48PM (#2838645)
Just including vprintf is not enough. Here are some timings (GCC-4.2.1, system compiler on OpenBSD-5.0-current/amd64 on a Thinkpad SL510; all timings are median-of-three):

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!

Re:PyPy is faster than C, again: string formatting by leon_ on Tuesday August 02, 2011 @03:34PM (#2838417)
> However, every time I see "faster than C" almost always a bit of trickery/wordplay is involved.

yeah, it's almost like those "$something in only $few lines of $programming_language" posts where you get highly obfuscated code to read.