Thread Tools Display Modes
03-08-12, 04:18 AM   #1
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Related to string finding

Thread -> http://eu.battle.net/wow/en/forum/topic/3429936834

Subscription expired, but was curious to ask for one thing:

What is most efficient, for example to use when hooking the chat messages incoming to parse for specific things:
Code:
if ("a !teST message"):find("![tT][eE][sS][tT]") then ... end
...or to convert it to lower case first:
Code:
if ("a !teST message"):lower():find("!test") then ... end
If I run 100 addons using each way, I wonder what would be less of a FPS drop, hmm.
  Reply With Quote
03-08-12, 04:37 AM   #2
Talyrius
An Onyxian Warder
 
Talyrius's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 363
In your second example, you're adding an extra step (the lower() function), so that's adding overhead. However, the difference between the two is unlikely to even be accurately measurable. Use whichever way you prefer.
  Reply With Quote
03-08-12, 04:37 PM   #3
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
(I find the :lower one more readable, even the more if the pattern is going to be very long)

Looking forward for someone to do some actual performance tests for us though ..
  Reply With Quote
03-09-12, 01:22 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
I don't have time to run it at the moment, but here's a modified version of a comparison function I used in another thread. If nobody replies with the printed results, I'll run it later tonight.

lua Code:
  1. local function func1()
  2. --  Put Function 1 code here
  3.     return ("a !teST message"):find("![tT][eE][sS][tT]");
  4. end
  5.  
  6. local function func2()
  7. --  Put Function 2 code here
  8.     return ("a !teST message"):lower():find("!test");
  9. end
  10.  
  11. --  These are run a lot, need local pointers
  12. local math,debugprofilestart,debugprofilestop=math,debugprofilestart,debugprofilestop;
  13. SlashCmdList.FUNCTEST=function()
  14.     local l1,l2,h1,h2;
  15.     local s1,s2,c1,c2=0,0,0,0;
  16.     for i=1,1000000 do
  17.         local t1,t2;
  18.  
  19. --      Mix up the order these functions are run (Sometimes this affects run times)
  20.         if i%2>0 then
  21.             debugprofilestart(); func1(); t1=debugprofilestop();
  22.             debugprofilestart(); func2(); t2=debugprofilestop();
  23.         else
  24.             debugprofilestart(); func2(); t2=debugprofilestop();
  25.             debugprofilestart(); func1(); t1=debugprofilestop();
  26.         end
  27.  
  28. --      Add to total times
  29.         s1=s1+t1;
  30.         s2=s2+t2;
  31.  
  32. --      Compare which ran the shortest and increment relevant "Best" count
  33. --      Note: Times can be equal, in which ignore and calculate "Equal" count later
  34.         if t1<t2 then
  35.             c1=c1+1;
  36.         elseif t1>t2 then
  37.             c2=c2+1;
  38.         end
  39.  
  40. --      Set/Update min/max vars
  41.         if not l1 then
  42.             l1,h1,l2,h2=t1,t1,t2,t2;
  43.         else
  44.             l1=math.min(l1,t1);
  45.             l2=math.min(l2,t2);
  46.             h1=math.max(h1,t1);
  47.             h2=math.max(h2,t2);
  48.         end
  49.     end
  50.  
  51. --  Print results
  52.     print("Totals",s1,s2);
  53.     print("Avg",s1/1000000,s2/1000000);
  54.     print("High",h1,h2);
  55.     print("Low",l1,l2);
  56.     print("Best",c1,c2);
  57.     print("Equal",1000000-c1-c2);
  58. end;
  59.  
  60. SLASH_FUNCTEST1="/functest";
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
03-11-12, 06:35 AM   #5
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by SDPhantom View Post
I don't have time to run it at the moment, but here's a modified version of a comparison function I used in another thread. If nobody replies with the printed results, I'll run it later tonight.
Since you didn't post it yet, here is my dump of running your code
Code:
Totals 669.73373221 970.02821804133
Avg 0.00066973373221 0.00097002821804133
High 0.2507797084845 0.39165764962977
Low 0.00042433114802792 0.00084866229605584
Best 531082 954
Equal 467964
Code:
Totals 657.32374345437 983.88305435578
Avg 0.00065732374345437 0.00098388305435578
High 0.26945027899773 3.0530626100609
Low 0.00042433114802792 0.00084866229605584
Best 544513 1144
Equal 454343
  Reply With Quote
03-11-12, 01:01 PM   #6
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
It seems it's better to not use :lower() if you can avoid it and rather use pure regex (as simple as that one) but maybe if you have a very long regex it may become slower than using :lower(), not sure, must try adding more matchers (brackets) and see how slower it gets if you double the amount.

Still interesting results!
  Reply With Quote
03-11-12, 04:23 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Still, the speed difference won't be noticable under normal circumstances (eg. using it in a live addon, instead of running it in a loop that repeats 1 million times), so you should just use the one that's easier for you to read.

When you come back to that section of your code after months of not looking at it, the last thing you want to do is spend a lot of time figuring out what a regex pattern does (or is supposed to do).
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Related to string finding


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