Skip to content

Conversation

@RocwoDev
Copy link
Owner

Here the performance test

function math.Timer()
    local timer = os.clock()
    return function()
        return (os.clock() - timer)
    end
end

local round1 = function(value, numDecimalPlaces)
	if numDecimalPlaces then
		local power = 10^numDecimalPlaces
		return math.floor((value * power) + 0.5) / (power)
	else
		return math.floor(value + 0.5)
	end
end

local round2 = function(num, numDecimalPlaces)
    return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

local timer1 = math.Timer()
for i = 1, 1000000 do
     round1(1883.948364, 2)
end
print(timer1()) -- 0.077

local timer2 = math.Timer()
for i = 1, 1000000 do
     round2(1883.948364, 2)
end
print(timer2()) -- 1.176

@RocwoDev RocwoDev merged commit 82b7fd3 into main Apr 16, 2024
@RocwoDev RocwoDev deleted the ArdentLeKhey-performance-patch-round branch April 16, 2024 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants