View Single Post
08-20-14, 11:28 AM   #10
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Originally Posted by Resike View Post
It's not always true, keeping a local var inside the for loop:

- Create the variable
- Assign memory address to it.
- Allocate the memory for it.

- Then upvalue it with something.

However if you keep it outside the for loop, then the first 3 step is gonna run when you load the addon and only then, not recreating them at each loop, make the for loop run faster, since it only gonna overwrite whatever it can find the assigned memory address.
(Quick note with the current memory modules (DDR2-DDR3) overwriting a value is 30% faster then completely clear a memory block clean.)
However accessing a variable outside the loop also take some extra time.
You don't really make the whole code run faster, however you spread the workload into more modules, making the CPU/Memory peak usage lower.

More info in this thread:

http://www.wowinterface.com/forums/s...t=47694&page=3
Well I don't know how this stuff works, but that doesn't seem to be true. I'm guessing it's not to simple as "replacing" the value of the memory address if the new value is much bigger, for example?

Anyway, I got the same results as in your post there, but you forgot the format that people actually use. That is, assigning the values directly instead of declaring the variables separately. Unless I did something wrong, that was a lot faster. (302 vs 546 ms at 100 000 iterations)

Edit: Yup. Upvalues are actually significantly slower.
Code:
local function func()
	return 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9
end

do
	local time = debugprofilestop()

	local q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l
	for i = 1, 1e6 do
		q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l = func()
	end

	print(debugprofilestop() - time)
end

do
	local time = debugprofilestop()

	for i = 1, 1e6 do
		local q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l = func()
	end

	print(debugprofilestop() - time)
end
235 vs. 161 ms.
__________________
Grab your sword and fight the Horde!

Last edited by Lombra : 08-20-14 at 11:38 AM.
  Reply With Quote