Page 2 of 2

Re: "Adding" a number to an variable

Posted: Fri Apr 27, 2012 9:04 am
by Roland_Yonaba
Ensayia wrote:Lua lets you do quite a few more things with easy shortcuts than any other language. I really feel spoiled by it, it would probably be hard for me to start learning another language. There's no way C, C++, Java, etc would allow you to do something that elegant...
I.Totally.Agree.

Re: "Adding" a number to an variable

Posted: Fri Apr 27, 2012 2:27 pm
by timmeh42
What was wrong with the suggestion of just multiplying by 10 and adding the number?

Re: "Adding" a number to an variable

Posted: Fri Apr 27, 2012 3:12 pm
by tentus
It's very marginally faster to do the concat way (100,000 reps each comes to .066 seconds for the multiply and add, while it only took .044 for the concat).

But beyond that I dunno.

Source:

Code: Select all

function love.load()
	x = 0
	sx = love.timer.getTime()
	for i=1,100000 do
		x = (x * 10) + 1
	end
	ex = love.timer.getTime()
	y = 0
	sy = love.timer.getTime()
	for i=1,100000 do
		y = tonumber(tostring(y).."1")
	end
	ey = love.timer.getTime()
end
function love.draw()
  love.graphics.print("It took " .. (ex-sx) .. " to multiply x 100,000 times", 5, 5)
  love.graphics.print("It took " .. (ey-sy) .. " to concatenate y 100,000 times", 5, 25)
end