lua-users home
lua-l archive

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


Hi,

Two possibilities for the speed difference.  Here are my
guesses.

for q=1,1000 do
        x = I + matrix.inv(x)
end

Is the above code generating a new 100x100 matrix at each
iteration? I'd be surprized if Matlab didn't optmize this to operate on the matrix 'x' itself, instead of creating a
new matrix and copying it over 'x'.  This eliminates a lot
of memory allocation, copying, and a lot of garbage
collection.

It woudl be *very* hard to optimize this in Lua. You'd need
at least two changes.

One is on the semantics of something like

    x = matrix(100, 100)
    y = x

I don't know if NumericLua does it, but you'd have to create
a copy of x and place a reference to it on y, instead of
just pointing y to x.  It would be nice if you did this by
copy-on-write.  Matlab does something similar to that with
function arguments.

The problem is that you'd still need a change to Lua
metametods. Perhaps Lua 5.2 could have it. We'd need access to an optional l-value. In expressions such as

    x = a  +  b + c
    x = (a + b) + c

the first operation would have changed x. The second would
receive x both as a target and a source. This means you'd have to implement your operations in such a
way that the source and target can be the *same*, which is
not trivial for things like matrix multiplication.

for i,j in x:entries() do
		x[i][j] = math.random()
end

I didn't look at the code, but the syntax above suggests you
are creating a temporary object for x[i], which then
receives the [j] indexing. This has got to be slower than
doing something like x(i, j). I know the syntax looks
better, but I x(i, j) is also fine and could be *much*
faster.