View Single Post
Old 05-20-2020, 01:44 PM   #5
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

Parsing strings in Lua:

When parsing through strings, using string.gmatch:

Code:
-- parse line by line
for found_part_string in string.gmatch(mystring, "(.-)\n") do
   ...
end
is usually much faster than using the match-function:

Code:
-- parse line by line
while mystring:match("(.-)\n")~=nil do
  found_part_string = mystring:match("(.-)\n") -- get the next line into found_part_string
  mystring=mystring:match("\n(.*)")               -- remove found line from mystring, so we can find 
                                                                   -- the next line in the next loop iteration
  ...
end
It is also better to avoid complex matching-patterns but rather match them in several steps:

1) try to find the bigger part(s) who probably contain the stuff you are looking for
2) patternmatch within the found parts the more detailed ones.

This is much faster, often by magnitudes.

@amalgama
Maybe, we should also collect in here some other hints, bits and pieces in improving code-speed. I guess we all have lessons learnt on a code-strategy-way, who can influence speed of scripts significantly.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote