Re:PyPy is faster than C, again: string formatting
by apaprocki on Tuesday August 02, 2011 @05:35PM
(#2838810)
Interesting.. I guess I should have mentioned gcc 4.5.2 on a Xeon X5670, 2.93GHz, which is 12M cache. Changing it to ('0' + j % 10) has no change in overall speed for me.
Re:PyPy is faster than C, again: string formatting
by 1amzave on Tuesday August 02, 2011 @05:48PM
(#2838844)
OK, I just tested with gcc 4.6.0, and unless I've screwed something up, it looks like (at -O4) it actually optimizes these into the exact same code. As in the generated ELFs are byte-for-byte identical. Impressive.
Re:PyPy is faster than C, again: string formatting
by apaprocki on Tuesday August 02, 2011 @06:28PM
(#2838965)
Since you showed interest, using this as the for loop tweaks it even a bit more :)
/* temp number */
char tmp[20];
int pos = 0;
int j = i > 0 ? i : -i;
do {
tmp[pos++] = '0' + j % 10;
j /= 10;
} while (j != 0 && pos <= 20);
/* output both numbers simultaneously */
char x[44], *p1 = x, *p2 = x + 1 + pos;
if (i < 0) { *p1++ = '-'; *p2++ = '-'; }
do {
int tpos = --pos;
*p1++ = tmp[tpos]; *p2++ = tmp[tpos];
} while (pos > 0);
*p1 = ' ';
*p2 = '\0';
Re:PyPy is faster than C, again: string formatting
by 1amzave on Tuesday August 02, 2011 @06:36PM
(#2838981)
Well, if you really want to go apples-to-oranges and specialize as much as possible for the sequentially increasing case we're dealing with here, you could just do your arithmetic directly on the ASCII string itself, complete with a little ripple-carry loop...I bet that'd be fast. If I'm remembering correctly, I think x86 actually has dedicated instructions for ASCII arithmetic (though they're probably so unoptimized in a modern microarchitecture it'd probably be faster to avoid them).
Edit: This was with GCC 4.1.2, newer versions probably optimize differently, so who knows.