WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Related to string finding (https://www.wowinterface.com/forums/showthread.php?t=42989)

Vlad 03-08-12 04:18 AM

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.

Talyrius 03-08-12 04:37 AM

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.

Ketho 03-08-12 04:37 PM

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

SDPhantom 03-09-12 01:22 PM

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";

Ketho 03-11-12 06:35 AM

Quote:

Originally Posted by SDPhantom (Post 253590)
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


Vlad 03-11-12 01:01 PM

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

Still interesting results!

Phanx 03-11-12 04:23 PM

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). :p


All times are GMT -6. The time now is 09:34 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI