Rüdiger
Rüdiger

Reputation: 943

Can LuaJIT be faster than C?

Is there a scenario where the LuaJIT might be executing code even faster than C?

After searching I found some people talking about the fact that it can at least rival the C-code, but I have never seen/found an example and I cannot inmagine how to create a setting where Lua is faster than C, since Lua seems to rely on C-libraries.

Does anyone have (had) a scenario where this happens and/or can show me an example where Lua is faster than C-Code?

Upvotes: 1

Views: 8019

Answers (3)

Viktor Nagy
Viktor Nagy

Reputation: 29

As I see it, it cant be faster (in general).

Both compiled and interpreted languages have their advantages, while interpreted ones are more portable and can be altered during runtime, compiled ones are faster (I am aware that this simplification is not exactly right but its imho good estimate).

JIT is "something in between" and you could get uni course about it, but it has some code interpretation making it generally slower. You can get close to "compiled language speed" but never quite 100%.

That being said, its important to compare apples to apples, so equally implemented code, you could absolutely write slower C code that computes the same thing that lua code does.

Upvotes: 1

sneusse
sneusse

Reputation: 1691

You're mixing Lua and LuaJIT. The first one is a pure interpreter and nowhere near 'C performance'.

In LuaJIT on the other hand, while still beeing built with C, the performance critical parts are directly implemented in an assembly language (DynASM, also related to the LuaJIT project). Also, as the name suggests, your Lua code can be JIT compiled if the tracer decices that's the right thing to do.

Here you'll find some (maybe slightly biased) code of how LuaJIT can be extremely fast for table lookups and outperforms C/C++ at least in this case. https://gist.github.com/spion/3049314

Upvotes: 6

brianolive
brianolive

Reputation: 1671

As documented here, Lua is implmented in C. It can only be as fast as C, but is more likely to be slower. It can’t be faster than the language of its own implementation.

Upvotes: -2

Related Questions