Thread Tools Display Modes
07-25-11, 02:42 PM   #1
Pixol
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 13
Concatenation in a For Loop

My add-on currently has a high cpu usage
(according to Addon Profiler add-on)
I have commented out the majority of the script and found out this is the cause for major CPU usage, ~2CPU/sec. It's not as high as pitbull using like 20 but it's high for a simple task.

Code:
      for i=1,8 do
        for j=1,5 do
          x="CompactRaidGroup"..i.."Member"..j
        end
      end
Is there another other option than writing conditionals like below 40 times?
if i==1 and j==1
then x=CompactRaidGroup1Member1

Or is concatenation supposed to be a fairly high CPU usage task?
  Reply With Quote
07-25-11, 02:58 PM   #2
Pixol
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 13
Nevermind.
http://lua-users.org/wiki/StringsTutorial

Turns out using:
Code:
string.format("CompactRaidGroup%dMember%d", i,j)
is more CPU efficient
  Reply With Quote
07-25-11, 02:58 PM   #3
Starinnia
Ninja Code Monkey
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 84
You could save the concatenated strings in a table and retrieve them the next time you need the strings instead of always performing the concatenation.
  Reply With Quote
07-25-11, 03:02 PM   #4
Pixol
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 13
Originally Posted by Starinnia View Post
You could save the concatenated strings in a table and retrieve them the next time you need the strings instead of always performing the concatenation.
Hmm sounds like a good idea. I'll try that with the stringformat and see if it makes it even more efficient. It's down to about ~0.30 now from 2

Update: Table method seems to be more efficient than calling stringformat everytime. Thanks cpu ~ 0.25 now
Was a good review on 2dimensional arrays
http://www.lua.org/pil/11.2.html

Last edited by Pixol : 07-25-11 at 03:24 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Concatenation in a For Loop


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off