I wonder why a lot of programmers claim that Lua is faster then any other scripting language?
What did they do that is more efficient then other languages?
Is there something completely different in their approach?
What makes their code run faster then Python for example?
views:
5934answers:
7First and foremost: where did you see this claim about speed?
Anyway, a wild guess: a simpler and smaller language, with cleaner semantics and a small number of orthogonal mechanisms eliminates many special cases that a larger language, such as Python, must handle.
From Wikipedia:
Lua programs are not interpreted directly from the textual Lua file, but are compiled into bytecode which is then run on the Lua virtual machine. The compilation process is typically transparent to the user and is performed during run-time, but it can be done offline in order to increase loading performance or reduce the memory footprint of the host environment by leaving out the compiler.
Like most CPUs, and unlike most virtual machines (which are stack-based), the Lua VM is register-based, and therefore more closely resembles an actual hardware design. The register architecture both avoids excessive copying of values and reduces the total number of instructions per function. The virtual machine of Lua 5 is the first register-based VM to have a wide use.[4] Parrot (currently in development) is an another well-known register-based VM.
Lua also seems to be a smaller language than e.g. Python which has to take care of much more situations (metaprogramming).
About the claim: it seems that some benchmarks like the Computer Language Benchmarks Game shows that, for most programs, it is faster than most interpreted languages.
Why? I am not a specialist, but I know the language have been designed by a small number of people (but hearing remarks and suggestions from users), using a carefully hand-tuned parser and VM, with a garbage collector designed for speed (to be usable in games), etc.
It was designed for very light-weight embedding. Lua as an executable is about 50k and comes with almost no libraries. It's design to be a scripting language to sit on top of C so it doesn't bother with a full regex parser or even a socket library. It's faster than other interpreted languages because it was optimised for speed rather than convenience.
This is not to say it's necessarily the FASTEST interpreted language either. There are other less well known languages like IO and angelscript that can give lua a run for its money in speed benchmarks.
Lua has some very nice language features which allow implementers to compose higher level language features from these core features:
- function closures, multi-return, and proper tail-recursion - together these allow Lua to operate as a functional language, even performing list operations on varargs and multi-return ranges and efficient use of the stack. Lua's multi-return in the standard library to define a very light iterator pattern which can be implemented by user functions with or closures depending on how you feel about side-effects.
- mutable closures - function references in Lua define closures on the variables defined in that scope, and allow changes to these (unlike pure functional languages), this allows implementers to achieve OO data encapsulation.
- replaceable metatable - all values in Lua have a standard metatable which defines operators on that value, including index and newindex and with lua's fake-OO call notation this lets implementers achieve OO polymorphism
This means that if a particular programming methodology (functinoal, OO, or dynamic) is proves best for your situation, then you can use it.
On the other hand, if you want to make a small benchmark you're not really burdened by these features. Clearly python isn't a deficient language, and pypy's stackless API has some features I find very compelling that Lua doesn't yet have (tho with coroutines it could be implemented in a library).
If you find Lua hard to pick up, or prefer a more procedural syntax, try Agena. It's author says,
Agena is based on the ANSI C source code of Lua, a popular and widely used OpenSource programming language.