View Single Post
08-27-13, 08:30 PM   #31
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
Well, if you need a var in the OnUpdate or something happened frequently, I suggest you may try the coroutine. Also a test example :

1. First part is using a local var 'sum' to contains the sum result. And then we call it 10000 times.
2. Second part is using a coroutine to keep everything, we also can it 10000 times.

Lua Code:
  1. do
  2.     oclock = os.clock
  3.  
  4.     do
  5.         local sum = 0
  6.  
  7.         function Sum(num)
  8.             for i = 1, num do
  9.                 sum = sum + i
  10.             end
  11.         end
  12.  
  13.         function callSum()
  14.             collectgarbage()
  15.             local startTime = oclock()
  16.  
  17.             for i = 1, 10000 do
  18.                 Sum(i)
  19.             end
  20.  
  21.             local finsih = oclock()
  22.  
  23.             print("Result is", sum)
  24.             print("Up-value cost ", finsih - startTime)
  25.         end
  26.  
  27.         callSum()
  28.     end
  29.  
  30.     print("----------------------------")
  31.  
  32.     do
  33.         create = coroutine.create
  34.         resume = coroutine.resume
  35.         running = coroutine.running
  36.         status = coroutine.status
  37.         wrap = coroutine.wrap
  38.         yield = coroutine.yield
  39.  
  40.         function Sum(num)
  41.             local sum = 0
  42.  
  43.             while num do
  44.                 for i = 1, num do
  45.                     sum = sum + i
  46.                 end
  47.                 num = yield()
  48.             end
  49.  
  50.             print("Result is", sum)
  51.         end
  52.  
  53.         function callSum()
  54.             collectgarbage()
  55.             local startTime = oclock()
  56.             local thread = create(Sum)
  57.  
  58.             for i = 1, 10000 do
  59.                 resume(thread, i)
  60.             end
  61.  
  62.             resume(thread)
  63.  
  64.             local finsih = oclock()
  65.  
  66.             print("Thread cost ", finsih - startTime)
  67.         end
  68.  
  69.         callSum()
  70.     end
  71. end

The result is :

Result is 166716670000
Up-value cost 1.406231
----------------------------
Result is 166716670000
Thread cost 0.853961
Normally, like an OnUpdate handler, you can do it like :

Lua Code:
  1. local thread = create(function(...)
  2.     local var1, var2
  3.  
  4.     while true do
  5.         -- Handle your code
  6.         yield()
  7.     end
  8. end)
  9.  
  10. frame:SetScript("OnUpdate", function(self)
  11.     resume(thread)
  12. end)

So, when your frame is visible, the thread will be called again and again, when not, the thread will be stop until the frame is shown again.

But, if your handle code is tiny, just use upvalue, in the previous example, if you change the code
Lua Code:
  1. for i = 1, num do
  2.     sum = sum + i
  3. end
To
Lua Code:
  1. sum = sum + num

The result should be

Result is 50005000
Up-value cost 0.0021880000000003
----------------------------
Result is 50005000
Thread cost 0.0052459999999996
So, the choose is based on your code, and the coroutine is more complex than the upvalue, if you do it wrong, it may crash your game.
  Reply With Quote