"Adding" a number to an variable

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)

Re: "Adding" a number to an variable

Post by Roland_Yonaba » Fri Apr 27, 2012 9:04 am

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...

User avatar
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

Re: "Adding" a number to an variable

Post by timmeh42 » Fri Apr 27, 2012 2:27 pm

What was wrong with the suggestion of just multiplying by 10 and adding the number?

User avatar
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia

Re: "Adding" a number to an variable

Post by tentus » Fri Apr 27, 2012 3:12 pm

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.


Code: Select all

function love.load()
	x = 0
	sx = love.timer.getTime()
	for i=1,100000 do
		x = (x * 10) + 1
	ex = love.timer.getTime()
	y = 0
	sy = love.timer.getTime()
	for i=1,100000 do
		y = tonumber(tostring(y).."1")
	ey = love.timer.getTime()
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)
Kurosuke needs beta testers

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests