[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua string
- From: David Given <dg@...>
- Date: Sat, 28 Apr 2007 22:47:41 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Philippe Lhoste wrote:
> I disagree. What you call "same algorithm" is actually a naive rewriting
> of a language idiom into another language.
Respectfully, that's *why* it's the same algorithm. If you rewrote it to use a
more appropriate language idiom, it would be a different algorithm, more or
less by definition.
That's not to say that using the same algorithm into two different languages
sufficiently similar that getting such different performances is still surprising.
(And it's worth mentioning that the naive string concatenation algorithm is
lousy in both languages. SpiderMonkey just has a specific optimisation to
allow lousy code to run reasonably fast.)
> for (i = 0; i < strlen(strBigData); i++)
> in C: it is natural and intuitive.
> And very inefficient:
gcc specifically optimises this. strlen() is declared as pure, which means
that constant propagation can push the strlen() out of the loop. In fact, on
gcc 4.1.2 i386, the compiler has special knowledge of how strlen() works and
emits inline code using repnz scasb, so it doesn't even need an explicit loop...
> These are obvious to experienced C programmers, but I saw such code in
> real products...
I invite people to scan the archives of the Daily WTF. It's a website that
posts horrible pieces of code. They used to post one a day, but there was just
so much out there that they had to start posting more... it's quite a learning
┌── ｄｇ＠ｃｏｗｌａｒｋ．ｃｏｍ ─── http://www.cowlark.com ───────────────────
│ "Parents let children ride bicycles on the street. But parents do not
│ allow children to hear vulgar words. Therefore we can deduce that cursing
│ is more dangerous than being hit by a car." --- Scott Adams
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----