3 ms·
I'm not done, but I thought I'd share my quick benchmarks: 1 million strlens on the same random 100 byte string: Atom 330, 1.6GHz, gcc 4.3.2 (Debian Lenn
by jws 18y ago
I'm not done, but I thought I'd share my quick benchmarks:
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.
- jws 18y agoAnd to add more mystery: 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.
- DarkShikari 18y agoApple's libc is probably SIMD-optimized. You can make strlen a ton faster with some basic SSE instructions.
- jwilliams 18y ago... Yep - And OpenBSD is intended to run on as much hardware as possible - hence this simpler form might suit their purposes.
- dchest 18y agoNope, the have implementations for different architectures, you can find them here: http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/arch/ 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.
- jwilliams 18y agoThanks - sort of what I meant though :)
- dchest 18y agoHere's the source code for Apple's implementation (i386): http://www.opensource.apple.com/darwinsource/10.5.5/Libc-498.1.1/i386/string/strlen.s http://www.opensource.apple.com/darwinsource/10.5.5/Libc-498... (requires registration, here's gist link: http://gist.github.com/77178 http://gist.github.com/77178)