RegExReplace - Multiple Function calls VS Vertical bar "|" Alternatives Option, Which one is faster? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Guest

RegExReplace - Multiple Function calls VS Vertical bar "|" Alternatives Option, Which one is faster?  Topic is solved

19 Mar 2017, 11:34

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



In my system, "Multiple Function Calls" proved to be faster than using vertical bar "|" alternatives option

Another question is, if the RegExReplace is called 3 Times, as shown in the example below, does it mean that the text will be analyzed 3 times too?

TempText := RegExReplace(Text, "A", "#")
TempText := RegExReplace(TempText, "B", "#")
TempText := RegExReplace(TempText, "C", "#")

and what about using vertical bar "|" instead as shown below, will the text be analyzed 3 times or just 1 time?

TempText := RegExReplace(Text, "A|B|C", "#")
User avatar
nnnik
Posts: 2278
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: RegExReplace - Multiple Function calls VS Vertical bar "|" Alternatives Option, Which one is faster?

19 Mar 2017, 11:56

That is actually an interesting question:
Calling the function three times will lead to the text being parsed three times.
Calling it once with a more complex Needle only leads to the whole string being parsed once, but with a more complex search algorythm.
In any case it is prefereable to use the first Version since you don't actually need RegEx for the first ( use StrReplace instead ).
Recommends AHK Studio
Helgef
Posts: 2404
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegExReplace - Multiple Function calls VS Vertical bar "|" Alternatives Option, Which one is faster?

19 Mar 2017, 12:23

That is not a very good way to measure. The three calls seems slightly quicker though.

Related, but maybe not applicable in this case, see the S option for RegEx.

Cheers.
User avatar
jeeswg
Posts: 2666
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegExReplace - Multiple Function calls VS Vertical bar "|" Alternatives Option, Which one is faster?

19 Mar 2017, 13:39

@Guest. Interesting.
@Helgef. Did you have any better ideas for measuring apart from using QPC? Or repeating D, i.e. needles not in the haystack?
I had been planning to do some replace A-Z, StrReplace/RegExReplace tests. I.e. 26 v. 1 replacements out of interest.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Some tests on a-z:
(I think everything's OK, it's easy to make a silly mistake doing something like this.)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Helgef
Posts: 2404
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegExReplace - Multiple Function calls VS Vertical bar "|" Alternatives Option, Which one is faster?

20 Mar 2017, 11:08

@jeeswg, I don't think it is a good idea to include the gui operations in the measurements.
User avatar
jeeswg
Posts: 2666
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegExReplace - Multiple Function calls VS Vertical bar "|" Alternatives Option, Which one is faster?

21 Mar 2017, 21:36

@Helgef. Yeah, I couldn't tell what you were referring to in: 'That is not a very good way to measure.'

The inclusion of Gui commands *within* the start/end of the 'stopwatch' period is a little surprising, but the script and idea are good.

Btw have you ever benefited from the S option?
Regular Expressions (RegEx) - Quick Reference
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
Studies the pattern to try improve its performance.

@Guest. I wonder what made you suspicious about the RegEx speeds!

An old function I wrote, to replace letters, used StrReplace, I'd replaced it with a call to RegExReplace, but I thought that at some point I should double-check the speeds. I'm pretty sure I saw somewhere on the forum/documentation, a warning that RegEx can be slow. Although I think I'd also heard that Loop can be a bit slow.

Surprising results, from above, repeated here:

StringReplace CS/CI (26 replacements)
StrReplace CS/CI (26 replacements)
RegExReplace CS/CI (26 replacements)
RegExReplace CS/CI (1 replacement)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[hmm, interesting]
Acquiring high-resolution time stamps (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx

Return to “Ask For Help”

Who is online

Users browsing this forum: masheen and 30 guests