lua-users home
lua-l archive

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


On Wed, 2005-02-16 at 02:46, Mike Pall wrote:

> skaller wrote:
> > So? One is a primitive low level operation,
> > the other is a much higher level. C++ handles
> > stack unwinding, selective catching on type,
> > multiple levels of handlers, and decoupled
> > throwing and catching.
> 
> Sure, but I wasn't aware that it's _that_ slow. And unnecessary for the
> Lua core at least.

It isn't. Its faster. I just measured it at twice as fast,
but the reality is much better, C++ is almost certainly
100 times faster than C. The reason is obvious why this
must be so: setjump stores lots of registers in a memory
buffer to establish a handler. C++ probably pushes a single
pointer on the stack. C++ is the clear winner.

C++ -------------------------------
#include <setjmp.h>
#include <stdio.h>
int k;
void f() {++k; }

int main ()
{
  int i; jmp_buf x;
  for (i=0; i<1000000; ++i)
  {
    try { f(); } catch (...) {}
  }
  printf("%d\n",k);  
}

C ---------------------------------
#include <setjmp.h>
#include <stdio.h>
int k;
void f() {++k; }

int main ()
{
  int i; jmp_buf x;
  for (i=0; i<1000000; ++i)
  {
    setjmp(x);
    f();
  }
  printf("%d\n",k);  
}

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net