luajitã®å®å
追è¨
LuaJITã®ä½è
Mike Pallæ°ãããtwitterã§æ¬¡ã®ãããªã¢ããã¤ã¹ãããã ãã¾ããã
1. No compiler is allowed to make this optimization. Floating-point arithmetic ist NOT associative.
2. Please use 'local' functions when publishing Lua benchmarks.
3. Please use the current version of LuaJIT.
訳(ããªãæªãã)
1ï¼ãã®ãããªæé©ååºæ¥ãã³ã³ãã¤ã©ã¯ç¡ãããæµ®åå°æ°ç¹æ°ã®ç®è¡å½ä»¤ã¯çµåçãããªããã
2. Luaã®ãã³ããã¼ã¯ãåããªãå±æé¢æ°ã使ã£ã¦ãã ãã
3. ææ°ãã¼ã¸ã§ã³ã®LuaJITã使ã£ã¦ãã ãã
ããããããã§ããã³ããã¼ã¯ãåãç´ãã¾ãã
ãã³ããã¼ã¯ãããç´ãã¾ãããfunctionã®åã«localãå ¥ãã¦ãLuaJITãææ°ã«ãã¦ãã¾ããMike Pallæ°ã«ã¯ããã³ããã¼ã¯ã®ããç´ãã«ãããã¢ããã¤ã¹ãããã ãã¾ããããããã¨ããããã¾ãã
$ luajit -v
LuaJIT 2.0.1 -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
æé©åå
$ time luajit spline0.lua real 0m1.275s user 0m1.170s sys 0m0.031s
æé©åå¾
$ time luajit spline1.lua real 0m0.806s user 0m0.732s sys 0m0.015s
æé©ååã3%ã»ã©éããªã£ã¦æé©åå¾ã¨ã®å·®ãå°ã縮ã¿ã¾ãããJITç¡ãã®Luaã§ã®çµæã§ãã
$ time ./lua ../../luajit.org/spline0.lua real 0m1.273s user 0m1.185s sys 0m0.045s $ time ./lua ../../luajit.org/spline1.lua real 0m0.327s user 0m0.249s sys 0m0.046s
追è¨çµãã
ããã¯ãã¾ãã«ãã³ã³ãã¤ã©ã®æé©åã«æå¾ ãéãã§ããå®éã«åãåºããã³ã¼ããèªãã§ã¿ã¾ããããããªããã³ã³ãã¤ã©ã®ä½è ã ã£ãããããªããJITã®ä½è ã ã£ãããå ¥ã£ã¦æ¥ãã³ã¼ãããåããããªæé©åãã§ããã§ãããããã¾ãç¡çã§ããã©ããªé«åº¦ãªæé©åã³ã³ãã¤ã©ããæè©®ã¯äººéã®ä½ã£ãã³ã¼ãã§ããã³ã³ãã¤ã©ã¯ç¥ã§ã¯ãªãã®ã§ããããã¾ã§ã人éã®åµãã ããä¸å®å ¨ãªéå ·ã®ã²ã¨ã¤ã«éãã¾ããã
ãããããæé©å (http://d.hatena.ne.jp/shi3z/20130502/1367490202) ãã
ãããluajitãªããluajitãªããã£ã¦ããããã¨ä¿¡ãã¦ç¢ºããã¦ã¿ã¾ããã
çµæã
æé©åå
$ time luajit-2.0.0-beta10 spline0.lua real 0m1.268s user 0m1.200s sys 0m0.016s
æé©åå¾
$ time luajit-2.0.0-beta10 spline1.lua real 0m0.795s user 0m0.733s sys 0m0.046s
çè«å¤3åã®ã¯ããªã®ã§ããªãçãè¿ãã¦ãã¾ãã
ã¡ãªã¿ã«ãJITç¡ãã®luaã ã¨ãããªæãã
æéãããã£ã¦ãããããªãã®ã§ã«ã¼ãã1/100ã«ãã¦ãã¾ãã
æé©åå
$ time ./lua ../../luajit.org/spline0.lua real 0m1.309s user 0m1.216s sys 0m0.046s
æé©åå¾
$ time ./lua ../../luajit.org/spline1.lua real 0m0.331s user 0m0.249s sys 0m0.061s
ã¡ããã¨ãæé©åã¯åºæ¥ã¦ããããã§ãã
ã¾ã¨ã
ãluajitã¯ã¨ã¦ãé å¼µã£ã¦ãããå®ç§ã§ã¯ãªãã
移æ¤ããã½ã¼ã¹ã³ã¼ãã§ã
æé©åå
function catmullRom(p0, p1, p2, p3, t) local v0 = (p2 - p0) / 2.0 local v1 = (p3 - p1) / 2.0 return ((2.0 * p1 - 2.0 * p2) + v0 + v1) * t * t * t + ((-3.0 * p1 + 3.0 * p2) - 2.0 * v0 - v1) * t * t + v0 * t + p1 end function main(xp0, xp1, xp2, xp3, yp0, yp1, yp2, yp3, pp0, pp1, pp2, pp3) local d = math.sqrt((xp1 - xp2) * (xp1 - xp2) + (yp1 - yp2) * (yp1 - yp2)) local num = math.ceil((d / 5.0) + 0.5) local x,y,p local invertNum = 1.0/num local deltaT = 0 for i = 0, num do deltaT = deltaT + invertNum x = catmullRom(xp0,xp1,xp2,xp3, deltaT) y = catmullRom(yp0,yp1,yp2,yp3, deltaT) p = catmullRom(pp0,pp1,pp2,pp3, deltaT) end end for j = 0, 10000 do main(1.0, 100.0, 200.0, 200.0, 300.0, 100.0, 0.0, 200.0, 0.0, 100.0, 200.0, 300.0) end
æé©åå¾
function main(xp0, xp1, xp2, xp3, yp0, yp1, yp2, yp3, pp0, pp1, pp2, pp3) local dx = xp1-xp2 local dy = yp1-yp2 local d = math.sqrt(dx*dx+dy*dy) local num = math.ceil((d*0.2) + 0.5) local x,y,p local invertNum = 1.0/num local deltaT = 0 local xv0 = (xp2-xp0)*0.5 local xv1 = (xp3-xp1)*0.5 local xfact1=((xp1 - xp2)*2.0 + xv0 + xv1) local xfact2=((xp2 - xp1)*3.0 - 2.0 * xv0 - xv1) local yv0 = (yp2-yp0)*0.5 local yv1 = (yp3-yp1)*0.5 local yfact1=((yp1 - yp2)*2.0 + yv0 + yv1) local yfact2=((yp2 - yp1)*3.0 - 2.0 * yv0 - yv1) local pv0 = (pp2-pp0)*0.5 local pv1 = (pp3-pp1)*0.5 local pfact1=((pp1 - pp2)*2.0 + pv0 + pv1) local pfact2=((pp2 - pp1)*3.0 - 2.0 * pv0 - pv1) local xfact1n =0 local yfact1n =0 local pfact1n =0 local xFact1step = xfact1 * invertNum local yFact1step = yfact1 * invertNum local pFact1step = pfact1 * invertNum for i = 0, num do deltaT = deltaT + invertNum x =((xfact1n + xfact2) * deltaT + xv0) * deltaT + xp1 y =((yfact1n + yfact2) * deltaT + yv0) * deltaT + yp1 p =((pfact1n + pfact2) * deltaT + pv0) * deltaT + pp1 xfact1n = xfact1n + xFact1step yfact1n = yfact1n + xFact1step pfact1n = pfact1n + xFact1step end end for j = 0, 1000000 do main(1.0, 100.0, 200.0, 200.0, 300.0, 100.0, 0.0, 200.0, 0.0, 100.0, 200.0, 300.0) end