lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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:


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

- --
┌── ─── ───────────────────
│ "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
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla -