Hatena::Groupsubtech

冬通りに消え行く制服ガールは✖夢物語にリアルを求めない。

 | 

May 12, 2007 (Sat)

Lua math ライブラリが遅い? 03:35 はてなブックマーク - Lua math ライブラリが遅い? - 冬通りに消え行く制服ガールは✖夢物語にリアルを求めない。

math ライブラリが遅いって聞いたけど

#!lua
# math1.lua
for i = -5000000, 5000000 do
	math.abs(i)
end
#!lua
# mathrep.lua
function abs(i)
	if i < 0 then return -i else return i end
end

for i = -5000000, 5000000 do
	abs(i)
end
#!lua
# math2.lua
abs = math.abs
for i = -5000000, 5000000 do
	abs(i)
end
$ repeat 5 time lua -v math1.lua
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.68s user 0.02s system 99% cpu 2.719 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.68s user 0.01s system 99% cpu 2.710 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.68s user 0.01s system 99% cpu 2.712 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.68s user 0.01s system 99% cpu 2.711 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.68s user 0.01s system 99% cpu 2.711 total
$ repeat 5 time lua -v mathrep.lua
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v mathrep.lua  2.51s user 0.02s system 98% cpu 2.562 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v mathrep.lua  2.52s user 0.02s system 99% cpu 2.548 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v mathrep.lua  2.51s user 0.01s system 99% cpu 2.545 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v mathrep.lua  2.52s user 0.01s system 99% cpu 2.547 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v mathrep.lua  2.51s user 0.01s system 99% cpu 2.547 total
$ repeat 5 time lua -v math2.lua
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.17s user 0.01s system 99% cpu 2.195 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.17s user 0.01s system 99% cpu 2.192 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.17s user 0.01s system 99% cpu 2.198 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.17s user 0.01s system 99% cpu 2.191 total
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
lua -v math1.lua  2.17s user 0.01s system 99% cpu 2.193 total

結論:(少なくとも Mac 上の 5.1.1 の math.abs については) テーブルのキーをひくのがおそいだけで、関数自体は math のほうが高速

 | 

スポンサード リンク

書いてる人

cho45 (佐藤広央) (www.lowreal.net)

Perl, JavaScript, Ruby, HTML, CSS, Web etc


スポンサード リンク