Thursday, November 12

Geek

Allure

Code
import lua
import time

N=100000

t0=time.time()

def timer(f,n):
  t0=time.time()
  f(n)
  print f.__name__, time.time()-t0

def pyinc(n):
  a=0
  for i in range(n):
    a+=1

def pyclassinc(n):
  a=myclass()
  for i in range(n):
    a.inc()

def luainc(n):
  lua.execute('a=0')
  for i in range(n):
    lua.execute('a=a+1')

def luabulkinc(n):
  lua.execute('a=0')
  lua.execute('n=%s' % n)
  lua.execute('for i = 1,n do a=a+1 end')

def luaclassinc(n):
  lua.execute('a=python.eval("myclass()")')
  for i in range(n):
    lua.execute('a.inc()')

def luabulkclassinc(n):
  lua.execute('a=python.eval("myclass()")')
  lua.execute('n=%s' % n)
  lua.execute('for i = 1,n do a.inc() end')

def luasandboxclassinc(n):
  lua.execute('a=python.eval("myclass()")')
  lua.execute('python=nil')
  lua.execute('require=nil')
  for i in range(n):
    lua.execute('a.inc()')

class myclass(object):
  def __init__(self):
    self.value = 0
  def inc(self):
    self.value+=1

timer(pyinc,N)
timer(luainc,N)
timer(luabulkinc,N)
timer(pyclassinc,N)
timer(luaclassinc,N)
timer(luabulkclassinc,N)
timer(luasandboxclassinc,N)
Results
pyinc 0.0194
luainc 0.5534
luabulkinc 0.013
pyclassinc 0.071
luaclassinc 0.7851
luabulkclassinc 0.2597
luasandboxclassinc 0.7885
What does this all mean?

Well, first, Minx is getting a real scripting language. You can program stuff using the template language (real programming as opposed to just doing data selection and layout) but it gets hairy pretty fast.

Second, the scripting language runs faster than the application language - or would, except that the application is run using a JIT compiler. There's a JIT compiler for the scripting language too, but I don't know how to get that working as an embedded environment.

Third, the work I'm doing to make the Minx code simpler and more efficient - via a set of "magical" classes that do lazy evaluation for all the database-to-template-tag translation - transfers directly to the coming scripting language. There is an overhead involved - about 2.5μs per cross-language method call - so you don't want to do anything insanely complicated in the scripting language (or at least, not without caching the data in native variables).  And the overhead of transferring execution into the scripting environment in the first place is on the order of 5μs, so a few tens of thousands of script calls and things will start to add up...

Update: It pays to read the descriptions of all the branches; someone's already done what I've spent the past two hours failing to do, i.e. localise the Lua state.

Update: Of course, the branch that does what I want doesn't actually compile.  However, the changes in the branch that compiles don't interfere with the changes in the branch that does what I want...  I think.

Update: A bit more hickory-hackery* and it compiles and installs correctly.  The installation of the previous version prevent it from running correctly unless the interface file is copied into the working directory, but that's a minor issue; I can hunt down the old libraries and killerise them.

There may still be a problem with Lua 5.0 vs. 5.1; I'll take a look for that, since that's fixed in the other version.  I'll also note that the guy who did the changes to localise the interpreter state has done a better job of it than my hackish attempts, so many thanks to him.

Update: Aaand...  Working?  Seems to be!  Test results:
pyinc 0.0196
luainc 0.5294
luabulkinc 0.0135
pyclassinc 0.0692
luaclassinc 0.7695
luabulkclassinc 0.2589
luasandboxclassinc 0.7672
Possibly even a little faster, though the difference isn't significant.

Now to test it out with many interpreters.

Update: Many interpreters work fine; no obvious memory leaks.  However, creating a new interpreter context takes about 170μs; the 5μs I mentioned earlier was running a script inside an already-created environment.  Since most pages will only need one context anyway, that doesn't matter a great deal.  170μs is slow in computer terms, but pretty fast in human terms (about 250 times faster than the shortest interval you can notice).

* Editing the code with an axe.

Posted by: Pixy Misa at 12:20 AM | No Comments | Add Comment | Trackbacks (Suck)
Post contains 587 words, total size 5 kb.

Comments are disabled. Post is locked.
48kb generated in CPU 0.0488, elapsed 0.2491 seconds.
54 queries taking 0.2228 seconds, 336 records returned.
Powered by Minx 1.1.6c-pink.