lua-users home
lua-l archive

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


> Perhaps you would like to enlighten us all as to what
> you hoped to gain by asking such an apparently inane
> and obvious question? Were you hoping to catch us out,
> have you indeed caught me out -- is this all part of
> a devious plan to take over the world, or, as I
> suspect, are you simply a shining example of why I
> use Mutt on Linux, and you use Outlook on Windows?

I don't think this is necessary.  A simple statement that HTML messages
aren't welcome in this forum is sufficient.

As for the more substantive portion of your reply, Curt's asking for a
comparison between Lua and C++ is not inane or obvious.  Curt asked for
"performance comparisons" and there are many ways to measure performance--
not just processor cycles.

Maybe he cares about performance as it relates to code size.  Here, Lua will
usually win over C++ for larger projects.  Lua's VM opcodes are more
space-efficient than most any non-virtual processor because they operate at
a higher level of abstraction.  Of course, you have the overhead of the Lua
VM itself, but with careful slimming down, it can be quite small.

Maybe he cares about performance as it relates to run-time memory footprint.
Here, it is more difficult to say, but I found that Lua's memory allocation
and automatic garbage collection routines can (with suitable calls to the
garbage collector) keep things small.  And that's important in the embedded
systems world I'm usually in.  This observation is probably relative to my
C++ style, but I find I wind up chewing through more memory in C++ than in
Lua.  (Yes, that is a non-objective statement-- your mileage may vary.)

Maybe he cares about performance as it relates to his own efficiency.  Here,
there is no comparison-- I find I am vastly more productive in Lua than I am
in C++.  That's true of any high-level language, not just Lua, but Lua's
simple syntax and extensible nature let me do things I couldn't do in C++
(at least without tons of effort or the assumption of an underlying
operating system's services).

For performance as it relates to speed, the common assumption is that C++
would win.  But not so fast-- that won't always hold true either.  Let's
assume we're talking about a sophisticated real-world application (not a
stupid benchmark).  In many of the applications I write I want to have data
structures available like hash tables, and I want to have conversion
utilities so that I can easily do things like concatenate numbers to
strings.  I often want to be able to have arrays that can be resized at
run-time, and I want my memory allocation and reclamation to be as automatic
as possible-- so that I can focus on the problem, not the code.

So when I use C++, I use class libraries and templates that provide that
functionality.  Well once you start down that road, then the cost of Lua--
which has these and other features built in-- becomes less significant.
When you move from computing useless things like Ackerman's function to
actual real-world applications that do something useful, chances are you're
going to add to C++ the things that make your life easier from Lua.

C++ is going to win if you're doing largely useless benchmarks that
typically care only about processing cycles.  But when you start moving into
the real world, it isn't always a slam-dunk for C++.  There may be other
constraints.  Maybe you want to be able to read in source code and
dynamically extend your application.  C++ isn't ever going to do that, and
that essentially locks out a very useful programming technique from your
toolbox.  Or maybe the class-based multiple inheritance objects of C++
aren't what you need.  Lua allows you to create prototype-style objects, or
even pattern-based objects.  Actually, Lua lets you create whatever
semantics you want for objects.  Want objects that do class-based multiple
inheritance for code, but use a different model for data because that's what
makes sense in your application?  Isn't going to happen with C++ without
putting the equivalent of Lua's meta-mechanisms and flexible table data
structure.

Of course, it should be mentioned that comparisons between Lua and C++ are
kind of pointless.  Lua and C++ can be used together-- as many people in
this mailing list point out all the time.  One can easily do the
speed-critical processing in C++ and do the rest in Lua.  That gives you the
best of both worlds.