127 points · by aston · 17y ago · 70 comments
1 million strlens on the same random 100 byte string:
Atom 330, 1.6GHz, gcc 4.3.2 (Debian Lenny)
glibc: 1.3ns/char
easy: 3.4ns/char
obsd: 3.4ns/char
Core2 Duo, 2.8GHz, gcc version 4.0.1 (Apple Inc. build 5484)
libc: 0.12ns/char
glibc: 0.39ns/char
easy: 0.58ns/char
obsd: 0.60ns/char
easy: while ( *p++) c++;
obsd: openbsd, for (s = str; *s; ++s); return s-str;
The preliminary conclusions are:The glibc strlen is something like twice as fast as the naive implementation, but there is something else out there that knocks its socks off.
Secondary conclusion would be: remember not to compare GHz across different processors.
1 million strlens on the same random 100 byte string:
Pentium 4 3.0GHz, gcc 4.3.2 (Debian Lenny)
glibc: 0.52ns/char
libc: 0.60ns/char ???
easy: 0.94/char
obsd: 0.94/char
I would expect libc and glibc to match being a Debian machine, and they matched on the Atom 330. I can't account for the difference here, perhaps the difference in the Debian library compilation flags and my -O3 make a difference on this processor.Don't tell the Gentoo crowd, you'll only encourage them.
Also, http://funroll-loops.info/ for those who haven't seen it.
And I used to use Gentoo exclusively. :)
(requires registration, here's gist link: http://gist.github.com/77178)
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/arch/
The linked code is for the case when there's no arch-specific implementation.
Author covers really clever techniques to count bits, count non zero bytes like this etc etc. Bit manipulation at its best.
I agree with some of the reviewers though, the title probably means less people manage to find it than if it was called "Bit manipulation bible" or something.
http://www.daemonology.net/blog/2008-06-05-faster-utf8-strle...
I used to use and love FreeBSD (Since switched to OS X as desktop os, still run FreeBSD servers), but OpenBSD source code just looked more approachable.
McKusicks' wonderful book and video course helped.
The glibc version is written in such a way to minimize the jumps in the assembly and therefore produce faster code. Besides reading 4 bytes at a time, it does some clever (and very nontrivial) "magic." But again, all to reduce the number of jumps.
Don't in any circumstances refer to Unix source code for or during your work on GNU! (Or to any other proprietary programs.)
If you have a vague recollection of the internals of a Unix program, this does not absolutely mean you can't write an imitation of it, but do try to organize the imitation internally along different lines, because this is likely to make the details of the Unix version irrelevant and dissimilar to your results.
For example, Unix utilities were generally optimized to minimize memory use; if you go for speed instead, your program will be very different. You could keep the entire input file in memory and scan it there instead of using stdio. Use a smarter algorithm discovered more recently than the Unix program. Eliminate use of temporary files. Do it in one pass instead of two (we did this in the assembler).
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/arch/i386...
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/arch/amd6...
It uses repne scasb.
Comment in x86-65 directory says:
"change amd64's MACHINE_ARCH from x86_64 to amd64. There are many many reasons for this, quite a few of them technical, and not all of them in response to Intel's broken ia32e crud. The gcc toolchain stays at x86_64 for now."
How do they stack up in benchmarks?
Edit: See other posts in this discussion for actual benchmarks. >_>
getLength()
{
return length;
}The "right" answer was just to use strlen()...
element.getElementsByTagName(tagName);
That was what he was looking for. ;-) Of course, then he had me do it out as if getElementsByTagName didn't exist.Will I employ similar technique in my own code? Absolutely not. It hinders readability and byte comparison instructions in consecutive memory addresses are so stupidly fast that I'll probably save less CPU time combined in all executions of my program than the total amount of time it took me to think this hack up.
If you're a maintainer of glibc though (which is known for its exceptionally clear and straight-forward code,) then I might consider accepting a patch from someone who thought it up.
edit: I thought similarly of DJB's loop unrolling when I saw it in qmail. It's cute, but will it make any noticeable difference today? Probably not.
#ifdef NO_CLEVER_OPTIMIZATIONS
obvious version
#else
complicated fast version
#endif
It's good as documentation, good as a test case, and good for isolating weird problems like compiler optimization bugs.I'm just curious if the speed-up of this hack was ever benchmarked. Or if anyone spent a while tracking down a source of bottlenecks, and found it to be an inefficient strlen implementation.
Unfortunately though, this is slower than comparing 4 bytes at a time yourself.
Working bytes 1 at a time is pretty expensive. 4 at a time is great on a 32 bit cpu (As long as they're aligned).
(Also, you're not going to be reading off the end of a malloc'ed block, because essentially all malloc implementations, including the one in glibc, return blocks whose start address and size are multiples of the architecture's word size.)
The ((longword - lomagic) & himagic) should be ((longword - lomagic) & ~longword & himagic)
It relies on certain assumptions that are not the part of the C standard, so while this code works on the majority of platforms, this is not a portable C code. In which case going all the way down to the assembly level makes more sense. Especially considering there are typically dedicated CPU instructions for the exact purpose of searching a zero in a contiguous block of memory. Something like "rep scasb" on x86.
Also, using strlen() on a 4k string at all borders on malpractice.
"friendlier to the micro-architecture" doesn't even make sense. Check the chip timings for rep scasb. It's not friendly.
We're not really discussing wether you should be using strlen on large strings or not, but even if it's used say a million times on strings of length 80 or so, you'd see an improvement worth having.
Check any assembly language forum, book, etc and there will be discussion on why rep scasb/movsb/cmpsb are lame.
Would you implement string copy with rep movsb as well?