Author Message
skabet

Joined: 19 Nov 2007
Posts: 3

Posted: Mon Nov 19, 2007 9:50 am    Post subject: Performance: MiniD vs Lua

Hi,

I made a small test to see how fast MiniD is. I'm currently building a game engine, and we are currently planing on using a scripting language for a large part of the game logic. Our choice was first Lua, but then I saw this project on d newsboard, and decided to see if it's a good alternative. It's pretty oblivious that speed is importent when making a game engine, so the test was pretty small. Fib(30):

Lua:
 Code: function fib(n)   if n < 2 then     return n   else     return fib(n-1) + fib(n-2)   end end print(fib(30))

832040

real 0m0.279s
user 0m0.276s
sys 0m0.000s

MiniD:
 Code: module demo; function fib(n) {     if(n < 2)         return n;     else         return fib(n-1) + fib(n-2); } writefln(fib(30))

832040

real 0m2.327s
user 0m2.312s
sys 0m0.008s

A fast look at those numbers says that Lua is ~10 times faster then MiniD. When thinking about how many years and man-hours there have been used on Lua, is it pretty nice actually. Including the really nice syntax MiniD have.

But I'm still not sure on what to use, when i can get this speedup by using Lua. Are there any planing on making MiniD faster, or is it most features that's being working on? It would be really nice to see this scripting language get a tune up!

Anders Johnsen
skabet

Joined: 19 Nov 2007
Posts: 3

Posted: Mon Nov 19, 2007 2:14 pm    Post subject:

I was maybe a bit unfair in the last post. It's very rare I do recursive code. So I made a new little test with a iterative approach. The output is as following:

Lua:
 Code: function fib(n)     a = 0     b = 1     for i = 1, n-1 do         a,b = b,a+b     end     return b end for i = 0, 100000 do     fib(75) end

real 0m1.239s
user 0m1.236s
sys 0m0.004s

MiniD
 Code: module demo; function fib(n) {     local a = 0;     local b = 1;     for(local i = 0; i < n;i++)     {         local tmp = a;         a = b;         b = tmp+b;     }     return a; } for(local i = 0; i <= 100000; i++)     fib(75);

real 0m3.504s
user 0m3.496s
sys 0m0.008s

It's now 1/3 the speed as Lua - much better. So i guess the speed is okay. I'll try and make a last test, with some memory stuff. Maby a few tabels.
(LuaJIT gives on this one 0.233, but that's not really comparable, since it's JIT)
JarrettBillingsley

Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

Posted: Mon Nov 19, 2007 4:32 pm    Post subject:

In response to your PM, no it's perfectly OK for you to time MiniD

From the timings you've posted I take it you're running these tests as something like "time mdcl demo"? Keep in mind then that you're also taking into account the program startup and shutdown time. D, with its static module initializers and GC and all that fun stuff has a longer startup and shutdown time than an app written in C.

That being said, using the examples in your second post, the MiniD version actually runs slightly faster than the Lua version, if you only time the actual script execution. Furthermore, those for loops in the MiniD version are not the exact equivalent to the Lua for loops. If you use numeric for loops instead:

 Code: function fib(n) {    local a = 0;    local b = 1;    for(i: 0 .. n)    {       local tmp = a;       a = b;       b = tmp+b;    }    return a; } for(i: 0 .. 100000)    fib(75);

The MiniD version executes almost twice as fast as the Lua version. (to be honest I didn't even expect that..!)

This is actually still an unfair test, though, since all of Lua's arithmetic is with double-precision floats, while the MiniD code is using integers. (Although if you modify the code to use floats instead, it's still faster.)

As for my thoughts on speed: I am targeting MiniD to the same kinds of applications as Lua, and I aspire to one day have an interpreter as fast as Lua's. I'm always conscious of the performance impact that implementation details have, and will reject language features which, while interesting, would be prohibitively expensive to implement.

Even so, MiniD already blows the pants off Python in most areas, and lots of people use Python for writing web servers and games. Hell, people write web servers in Ruby, which is consistently the least performant scripting language out there. MiniD isn't as fast as Lua, but that's like saying a Corvette isn't as fast as a Porsche
vektorboson

Joined: 14 Sep 2006
Posts: 44

 Posted: Mon Nov 19, 2007 6:07 pm    Post subject: The original poster stated that he wants to use MiniD for a game; but reading the docs, it states that MiniD depends on D's Garbage Collector... As I see it, this would become a problem since MiniD could trigger a collection in the midst of the game, which is of course unacceptable (unless it's a turn-based game). Of course depending how scripting-heavy the game would become. So in my opinion memory management is a bigger problem than speed, since you always can implement functionality in D that would be too slow in MiniD. How hard would it be to implement reference counting in MiniD? Reference counting along with free lists would be nice (this would make a collection cycle less probable). Given that and a feature like Squirrel's suspendVM (I need a 'sleep' function), and I'd immediately change to MiniD.
JarrettBillingsley

Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

 Posted: Mon Nov 19, 2007 6:50 pm    Post subject: Since you can't overload * or ., refcounting in D (D1 at least) is not automatable, and so it would not be pretty or easy to use the MiniD library. Think "Python native API" and you'd be close. The only thing I can say is: Lua is used in tons of commercial games, and it has a GC. Furthermore, the quality of the D GC implementation is entirely up to the standard library and the developers. If a developer wanted to come up with their own GC, they could. One thing I'd add to support this, though, would be the ability to set a custom allocator for the MiniD library, much like Lua does. Don't know how tough that'd be. As for a "suspendVM" functionality.. well, since MiniD supports native coroutines, you could probably get the same effect by running your interpreter in a native coroutine, then yielding to suspend the VM. I'll have to look into that some more.
vektorboson

Joined: 14 Sep 2006
Posts: 44

Posted: Tue Nov 20, 2007 8:56 am    Post subject:

 JarrettBillingsley wrote: The only thing I can say is: Lua is used in tons of commercial games, and it has a GC. Furthermore, the quality of the D GC implementation is entirely up to the standard library and the developers. If a developer wanted to come up with their own GC, they could.

But there is quite a difference between the Lua GC and the GC used by MiniD. When Lua's GC runs, then it has to check how much? 200KB? 1MB? D's GC has to check the complete application, which may use as much as 512MB!

So Lua's GC may be run periodically (isn't it incremental?) while D's GC kills the game.
JarrettBillingsley

Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

 Posted: Tue Nov 20, 2007 12:41 pm    Post subject: I see your point. A D GC implementation that would allow multiple GCs in the same app for different memory management schemes (GCed for nonessential stuff, refcounted for the scripts, manual mgmt for something else etc.), and which would be incremental, would be fantastic. Someone's got to actually write it, though..
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 6 Hours Page 1 of 1