Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created November 24, 2015 06:24
Show Gist options
  • Save dermotbalson/e3343a549aee84c2d06c to your computer and use it in GitHub Desktop.
Save dermotbalson/e3343a549aee84c2d06c to your computer and use it in GitHub Desktop.
speed #2
local abs,cos,sin,tan,acos,asin,atan,deg,rad,floor,fmod,modf,max,min,pow,sqrt=math.abs,math.cos,
math.sin,math.tan,math.acos,math.asin,math.atan,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=5000000
local a,b,c,d=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 tbl={1,2,3,4}
local tbl2={}
tbl2={{1,2},{3,4},{5,6}}
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 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 TestTan() for i=1,n do local e=atan(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 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 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 e,f,g = a+d,b+d,c+d 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 TestCross() for i=1,n do a=v1:cross(v2) end end
local tests={
{TestAbs,"abs"},
{TestCos,"cos"},
{TestSin,"sin"},
{TestTan,"tan"},
{TestAcos,"acos"},
{TestAsin,"asin"},
{TestTan,"atan"},
{TestDeg,"deg"},
{TestRad,"rad"},
{TestFloor,"floor"},
{TestMax,"max"},
{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]"},
{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]"},
{TestMod2,"mod # [a%b]"},
{TestCross,"cross"}
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment