views:

5934

answers:

7

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?

A: 

First 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.

zvrba
Many, many programmers claim this here and on IRC.
the_drow
It's also a big reason that Lua has such a following in game development. The interpreter is easily embedded and perceived to be much faster than something like Python.
cookiecaper
+3  A: 

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).

Dario
Is there any differences in the code?I'm interested in their parser and vm.I'm investigating so I'll learn how to write my own scripting language for practice.
the_drow
Running from bytecode isn't really that unique; almost all VMs, even the JavaScript VMs in your browser, do that (PHP a notable exception; this is part of why PHP is so horrendously slow).
cookiecaper
cookiecaper: PHP does in fact have a byte-code intermediary. In plain vanilla PHP, it's dismissed right after evaluation, but you can install an opcode cache.
troelskn
+2  A: 

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.

PhiLho
+9  A: 

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.

SpliFF
I see that IO is the same as Lua with binding. I really dislike that bloated and verbose C code.Would you recommend AngelScript?
the_drow
I'd recommend Lua. I haven't used the others, just come across them in the past. The only program I know that uses AngelScript is the Apocalyx engine and I haven't seen anything that uses IO.
SpliFF
Lua imo is too complicated to bind/embed because of it's messy C interface. It looks too low level for something that complicated.I tried to use SWIG but I couldn't really figure it out.IO seems to bind the same way as Lua and AngelScript provides an easier, object oriented approch which I'm more used to.
the_drow
igouy
Lua's C interface is quite clean. SWIG is harder to check out. Have you read http://www.lua.org/pil/?
Norman Ramsey
As ioguy points out Lua is way faster than IO, even more than C is faster than Lua. IO is rather slow, ok average, nothing blazing about it
Robert Gould
@Norman: I disagree, it looks like PHP on system programming.That's just me though. For you it's clear.
the_drow
About Angelscript, it has a really great interface to register your classes your C++ classes and as the syntax is really close to C++, you can port nearly everything easily like enums, type hierarchies. Most of the time, you don't even need to do any type conversion between the language and C++. Also, it works quite well on a wide variety of console platforms. And last but not least, the author is still maintaining it actively and fixes bugs really quickly.
speps
+1  A: 

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).

Karl the Pagan
+23  A: 
Norman Ramsey
A: 

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.

boost
No thanks, I prefer OOP.But that's a nice scripting language.
the_drow
I'm pretty solid on JavaScript and I'm finding Lua really easy to pick up.
Jethro Larson