lua-users home
lua-l archive

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


The answer seems to be that LuaJIT doesn't like this sort of code. Lua 5.1.4 sees a roughly 2.5x slowdown on MacOS X 10.6 for the following code but LuaJIT 2.0-beta2 sees an almost 9x slowdown. It still beats Lua 5.1.4 by a wide margin (the sequence code in LuaJIT runs in about 60% of the open code in Lua 5.1.4), but apparently the tracing JIT can't manage to do as much with the sequence code as I might have hoped.

Mark

-----

local function timeIt( k, ... )

	if type( k ) ~= 'number' then
		return timeIt( 1, k, ... )
	end
	
	local clock = os.clock
	
	local start = clock()
	
	for i = 1, k do
		pcall( ... )
	end
	
	local finish = clock()
	
	return finish - start

end

local s = require "sequence"	-- See distribution from earlier today

local function plus_one( s, k ) return k + 1 end

local function is_odd( x ) return x % 2 == 1 end

local function square( x ) return x * x end

local function open_code( limit )
	local count = 0
	local total = 0
	for i in plus_one, 0, 0 do
		count = count + 1
		if limit < count then break end
		if is_odd( i ) then
			total = total + square( i )
		end
	end
	return total
end


local function seq_code( limit )
	local total = 0
	for v in s.seq( plus_one, 0, 0 )( s.take, limit )( s.filter, is_odd )( s.map, square )( s.iterator ) do
		total = total + v
	end
	return total
end

print( "Open code: ", open_code( 10000 ) )
print( "Sequence code: ", seq_code( 10000 ) )

print( "Open code time:", timeIt( 10000, open_code, 10000 ) )
print( "Sequence code time:", timeIt( 10000, seq_code, 10000 ) )