Skip to content

Commit ef9fb39

Browse files
whiterockAraq
authored andcommitted
added note to re constructor regarding performance (#13224)
Since I was new to regex I did not know that there is a compilation going on with ``re"[abc]"`` constructor and so I followed the other examples in the docs blindly, that is I just put the constructor directly in the arguments of match, find, etc., which was inside a loop and then wondered why my performance was so bad. Of course putting it outside the loop made it vastly more performant. People like me would benefit from the small note I added I would think :)
1 parent 2fad7f1 commit ef9fb39

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/impure/re.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ proc re*(s: string, flags = {reStudy}): Regex =
7878
##
7979
## Note that Nim's
8080
## extended raw string literals support the syntax ``re"[abc]"`` as
81-
## a short form for ``re(r"[abc]")``.
81+
## a short form for ``re(r"[abc]")``. Also note that since this
82+
## compiles the regular expression, which is expensive, you should
83+
## avoid putting it directly in the arguments of the functions like
84+
## the examples show below if you plan to use it a lot of times, as
85+
## this will hurt performance immensely. (e.g. outside a loop, ...)
8286
when defined(gcDestructors):
8387
result = Regex()
8488
else:

0 commit comments

Comments
 (0)