Log In  


I've noticed the min and max functions have a surprisingly high CPU cost.

For example

a=stat(1) for i=1,1000 do b=min(1,2) end print(stat(1)-a)

Outputs:0.103

Which is almost twice the CPU cost for sqrt()

a=stat(1) for i=1,1000 do b=sqrt(2) end print(stat(1)-a)

Outputs:0.054

And even if I define my own "min" function in lua, it's still a lot faster.

function mymin(a,b) return a<b and a or b end
a=stat(1) for i=1,1000 do b=mymin(1,2) end print(stat(1)-a)

Outputs:0.057

Is this a mistake?



min and max are defined in /system/lib/api.lua:

function max(a,b)
	if (type(a) != "number") a = 0
	if (type(b) != "number") b = 0
	return a > b and a or b
end

function min(a,b)
	if (type(a) != "number") a = 0
	if (type(b) != "number") b = 0
	return a < b and a or b
end

So it looks like it's slower cause it's doing some extra validation?


Makes sense. I had assumed they were built-in fns.
Turns out I was looking for math.min/math.max. (Too much Pico-8!)



[Please log in to post a comment]