[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua string
- From: David Given <dg@...>
- Date: Sat, 28 Apr 2007 22:47:41 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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
with different semantics is a good idea; but Javascript and Lua are
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:
Actually...
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
experience.
- --
┌── dg@cowlark.com ─── 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
iD8DBQFGM8D9f9E0noFvlzgRAksJAJ4ruR7BU/29F9XaTcM56L3k9zyXRgCfQ1Yo
9vlGSB2bhE+G3kvD4bcykdE=
=8tSj
-----END PGP SIGNATURE-----