Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active November 27, 2015 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/77afabbeff5dad53c31f to your computer and use it in GitHub Desktop.
Save dermotbalson/77afabbeff5dad53c31f to your computer and use it in GitHub Desktop.
Speed #3
local abs,cos,sin,tan,acos,asin,atan2,deg,rad,floor,fmod,modf,max,min,pow,sqrt=math.abs,math.cos,
math.sin,math.tan,math.acos,math.asin,math.atan2,math.deg,math.rad,math.floor,math.fmod,
math.modf,math.max,math.min,math.pow,math.sqrt
local fmod,modf,pack=math.fmod,math.modf,table.pack
n=3000000
local a,b,c,d,e,f=math.random()*100,math.random()*100,math.random()*100,
math.random()*100,math.random()*100,math.random()*100
local v1=vec3(math.random()*100,math.random()*100,-math.random()*100)
local v2=vec3(math.random()*100,math.random()*100,math.random()*100)
local v3=vec2(a,b)
local v4=vec2(c,d)
local tbl={1,2,3,4}
local tbl2={} for i=1,100 do tbl2[i]=i end
function setup()
counter=0
results={}
print("Testing..")
end
function TestAbs() for i=1,n do local e=abs(b) end end
function TestCos() for i=1,n do local e=cos(b) end end
function TestSin() for i=1,n do local e=sin(b) end end
function TestLocalSin() local s=math.sin for i=1,n do local e=s(b) end end
function TestTan() for i=1,n do local e=tan(b) end end
function TestAcos() for i=1,n do local e=acos(b) end end
function TestAsin() for i=1,n do local e=asin(b) end end
function TestAtan2() for i=1,n do local e=atan2(b,d) end end
function TestDeg() for i=1,n do local e=deg(a) end end
function TestRad() for i=1,n do local e=rad(a) end end
function TestFloor() for i=1,n do local e=floor(c) end end
function TestMax() for i=1,n do local e=max(b,c) end end
function TestMax2() for i=1,n do if a>b then max=a else max=b end end end
function TestPow() for i=1,n do local e=pow(a,d) end end
function TestSqrt() for i=1,n do local e=sqrt(b) end end
function TestSqrt2() for i=1,n do local e=b^0.5 end end
function TestExp() for i=1,n do local e=b^d end end
function TestIntDiv() for i=1,n do local e=b//a end end
function TestFmod() for i=1,n do local e=fmod(a,b) end end
function TestModf() for i=1,n do local e,f=modf(b) end end
function TestDist() for i=1,n do local e=v1:dist(v2) end end
function TestNormalize() for i=1,n do local e=v1:normalize() end end
function TestUnpack() for i=1,n do local e,f,g=v1:unpack() end end
function TestUnpack2() for i=1,n do local e,f,g=v1.x,v1.y,v1.z end end
function TestUnpack3() for i=1,n do local e,f,g=v1[1],v1[2],v1[3] end end
function TestPack() for i=1,n do local e=pack(a,b,c) end end
function TestPack2() for i=1,n do local e=vec3(a,b,c) end end
function TestVecAdd() for i=1,n do local e=v1+v2 end end
function TestVecMult() for i=1,n do local e=v1*a end end
function TestVecDivide() for i=1,n do local e=v1/a end end
function TestVecMult2() for i=1,n do local e,f,g = a*d,b*d,c*d end end
function TestVecDivide2() for i=1,n do local e,f,g = a/d,b/d,c/d end end
function TestVecAdd2() for i=1,n do local g,h,j = a+d,b+e,c+f end end
function TestLen() for i=1,n do local e = v1:len() end end
function TestLen2() for i=1,n do local e = (a*a+b*b+c*c)^0.5 end end
function TestMod2() for i=1,n do local e = b%d end end
function TestDot() for i=1,n do local e=v1:dot(v2) end end
function TestDot2() for i=1,n do local e=a*c+b*d end end
function TestCross() for i=1,n do local a=v1:cross(v2) end end
function TestFor() local t=0 for i=1,n do for j=1,100 do t=t+tbl2[j] end end end
function TestPairs() local t=0 for i=1,n do for _,p in pairs(tbl2) do t=t+p end end end
function InsertTable() local tt={} for i=1,n do for j=1,10 do table.insert(tt,j) end end end
function InsertTable2() local tt={} for i=1,n do for j=1,10 do tt[#tt+1]=j end end end
function TestAngleBetween() for i=1,n do local e=v3:angleBetween(v4) end end
function TestAngleBetween2()
for i=1,n do
local f=((a*a+b*b)*(c*c+d*d))^0.5
local e= acos((a*c+b*d)/f)
end
end
local tests={
--[
{TestAbs,"abs"},
{TestCos,"cos"},
{TestSin,"sin"},
{TestLocalSin,"sin local"},
{TestTan,"tan"},
{TestAcos,"acos"},
{TestAsin,"asin"},
{TestAtan2,"atan2"},
{TestDeg,"deg"},
{TestRad,"rad"},
{TestFor,"loop [for i=1,100 do]"}, --NEW
{TestPairs,"pair loop 100 times [for i,j in pairs(k) do]"}, --NEW
{InsertTable,"table.insert 10 items"}, --NEW
{InsertTable2,"insert 10 items in table with #"}, --NEW
{TestFloor,"floor"},
{TestMax,"max"},
{TestMax2,"max # [if a>b then max=a else max=b end]"}, --NEW
{TestPow,"pow"},
{TestSqrt,"sqrt"},
{TestSqrt2,"sqrt # [a^0.5]"},
{TestExp,"pow # [a^b]"},
{TestIntDiv,"floor # [a//b]"},
{TestFmod,"fmod"},
{TestModf,"modf"},
{TestDist,"dist"},
{TestNormalize,"normalize"},
{TestUnpack,"unpack"},
{TestUnpack2,"unpack # [a,b,c=v.x,v.y,v.z]"},
{TestUnpack3,"unpack # [a,b,c=v[1],v[2],v[3] ]"}, --NEW
{TestPack,"pack"},
{TestPack2,"pack # [v = vec3(x,y,z)]"},
{TestVecAdd,"vec3+vec3"},
{TestVecMult,"vec3*scalar"},
{TestVecDivide,"vec3/scalar"},
{TestVecMult2,"vec3*scalar # [x,y,z=x*a,y*a,z*a]"},
{TestVecDivide2,"vec3/scalar [x/a,y/a,z/a]"},
{TestVecAdd2,"vec3+vec3 # [x,y,z=x1+x2,y1+y2,z1+z2]"},
{TestLen,"len"},
{TestLen2,"len # [(x*x+y*y+z*z)^0.5]"},
{TestDot,"dot"},
{TestDot2,"dot # [x1*x2+y1*y2]"},
{TestMod2,"mod # [a%b]"},
{TestCross,"cross"},
{TestAngleBetween,"angle between vec"},
{TestAngleBetween2,"angle between x,y"}
}
function draw()
background(0)
if counter>0 then
if counter<=#tests then
results[counter]={DeltaTime,tests[counter][2]}
elseif counter==#tests+1 then
table.sort(results,function(a,b) return a[1]<b[1] end)
txt="--[[\nPerformance tests using "..format(n,0).." iterations\n"
for i=1,#results do
txt=txt.."\n"..format(n/60/results[i][1])..results[i][2]
end
saveProjectTab("TestResults",txt.."\n--]]")
sound(DATA, "ZgBAJgBHQEtAQEBApcaePlBxCT8qbzY+QABAc0BAQEBAQEBA")
print("All done!")
end
end
counter=counter+1
if counter<=#tests then
tests[counter][1]()
print(counter.."/"..#tests.." "..tests[counter][2].." - done")
end
end
local floor=math.floor
function format(amount)
local formatted = floor(amount/1000+0.5)*1000
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return string.sub(" ",1,7-string.len(formatted))..formatted.." = "
--return formatted..string.sub(" ",1,min-string.len(formatted))
end
--[[
local sin,rad=math.sin,math.rad
function SSS(d) return sin(rad(d)) end
local SSSS=SSS
function setup()
n=150000000
t=os.time()
for i=1,n do
local ss=S(37)
end
print(n/60/(os.time()-t))
local s,r=math.sin,math.rad
local SS=S
t=os.time()
for i=1,5000000 do
local ss=SS(37)
end
print(n/60/(os.time()-t))
t=os.time()
for i=1,5000000 do
local ss=SSS(37)
end
print(n/60/(os.time()-t))
t=os.time()
for i=1,5000000 do
local ss=SSSS(37)
end
print(n/60/(os.time()-t))
end
function draw()
end
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment