Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How is performance of this compared to Go's regex? #19

Closed
JensRantil opened this issue Apr 21, 2019 · 2 comments
Closed

How is performance of this compared to Go's regex? #19

JensRantil opened this issue Apr 21, 2019 · 2 comments

Comments

@JensRantil
Copy link

Have you done any basic benchmarks?

@JensRantil
Copy link
Author

Looking at https://godoc.org/github.com/dlclark/regexp2, I suspect this library is a lot slower since working with bytes tend to be a lot faster than strings.

@dlclark
Copy link
Owner

dlclark commented Apr 22, 2019

I have ported several benchmarks from the regexp package to regexp2 for comparison. You can find them in the regexp_performance_test.go file. Here's the output from my machine (2015 2.5Ghz i7 MacBook Pro)

BenchmarkLiteral-8                        	 3000000	       451 ns/op
BenchmarkNotLiteral-8                     	  300000	      4640 ns/op
BenchmarkMatchClass-8                     	 1000000	      1046 ns/op
BenchmarkMatchClass_InRange-8             	 1000000	      1036 ns/op
BenchmarkAnchoredLiteralShortNonMatch-8   	10000000	       158 ns/op
BenchmarkAnchoredLiteralLongNonMatch-8    	20000000	        98.5 ns/op
BenchmarkAnchoredShortMatch-8             	 5000000	       293 ns/op
BenchmarkAnchoredLongMatch-8              	10000000	       229 ns/op
BenchmarkOnePassShortA-8                  	 1000000	      1092 ns/op
BenchmarkNotOnePassShortA-8               	 1000000	      1130 ns/op
BenchmarkOnePassShortB-8                  	 2000000	       724 ns/op
BenchmarkNotOnePassShortB-8               	 2000000	       738 ns/op
BenchmarkOnePassLongPrefix-8              	 5000000	       318 ns/op
BenchmarkOnePassLongNotPrefix-8           	 5000000	       295 ns/op
BenchmarkMatchEasy0_32-8                  	20000000	        95.6 ns/op	 334.80 MB/s
BenchmarkMatchEasy0_1K-8                  	 5000000	       249 ns/op	4100.65 MB/s
BenchmarkMatchEasy0_32K-8                 	  200000	      7388 ns/op	4435.27 MB/s
BenchmarkMatchEasy0_1M-8                  	    5000	    278079 ns/op	3770.77 MB/s
BenchmarkMatchEasy0_32M-8                 	     100	  11788037 ns/op	2846.48 MB/s
BenchmarkMatchEasy1_32-8                  	10000000	       200 ns/op	 159.84 MB/s
BenchmarkMatchEasy1_1K-8                  	  300000	      5126 ns/op	 199.75 MB/s
BenchmarkMatchEasy1_32K-8                 	   10000	    146121 ns/op	 224.25 MB/s
BenchmarkMatchEasy1_1M-8                  	     300	   4805608 ns/op	 218.20 MB/s
BenchmarkMatchEasy1_32M-8                 	      10	 155318543 ns/op	 216.04 MB/s
BenchmarkMatchMedium_32-8                 	 3000000	       518 ns/op	  61.71 MB/s
BenchmarkMatchMedium_1K-8                 	  100000	     16009 ns/op	  63.96 MB/s
BenchmarkMatchMedium_32K-8                	    3000	    521474 ns/op	  62.84 MB/s
BenchmarkMatchMedium_1M-8                 	     100	  16629170 ns/op	  63.06 MB/s
BenchmarkMatchMedium_32M-8                	       2	 536617056 ns/op	  62.53 MB/s
BenchmarkMatchHard_32-8                   	  100000	     20548 ns/op	   1.56 MB/s
BenchmarkMatchHard_1K-8                   	    2000	   1010383 ns/op	   1.01 MB/s
BenchmarkMatchHard_32K-8                  	      50	  35005369 ns/op	   0.94 MB/s
BenchmarkMatchHard_1M-8                   	       1	1175492392 ns/op	   0.89 MB/s
BenchmarkMatchHard_32M-8                  	       1	38285374740 ns/op	   0.88 MB/s
BenchmarkMatchHard1_32-8                  	  500000	      2924 ns/op	  10.94 MB/s
BenchmarkMatchHard1_1K-8                  	   10000	    104998 ns/op	   9.75 MB/s
BenchmarkMatchHard1_32K-8                 	     500	   3484198 ns/op	   9.40 MB/s
BenchmarkMatchHard1_1M-8                  	      10	 110913710 ns/op	   9.45 MB/s
BenchmarkMatchHard1_32M-8                 	       1	3534549771 ns/op	   9.49 MB/s
BenchmarkLeading-8                        	  100000	     15429 ns/op
BenchmarkParserPrefixLongLen-8            	     100	  14529825 ns/op

And from the stdlib on the same box (go 1.12.1):

pkg: regexp
BenchmarkFind-8                           	10000000	       226 ns/op	       0 B/op	       0 allocs/op
BenchmarkFindAllNoMatches-8               	20000000	       121 ns/op	       0 B/op	       0 allocs/op
BenchmarkFindString-8                     	10000000	       222 ns/op	       0 B/op	       0 allocs/op
BenchmarkFindSubmatch-8                   	 5000000	       314 ns/op	      48 B/op	       1 allocs/op
BenchmarkFindStringSubmatch-8             	 5000000	       296 ns/op	      32 B/op	       1 allocs/op
BenchmarkLiteral-8                        	20000000	        78.3 ns/op
BenchmarkNotLiteral-8                     	 1000000	      1284 ns/op
BenchmarkMatchClass-8                     	 1000000	      1678 ns/op
BenchmarkMatchClass_InRange-8             	 1000000	      1646 ns/op
BenchmarkReplaceAll-8                     	 1000000	      1159 ns/op
BenchmarkAnchoredLiteralShortNonMatch-8   	20000000	        64.7 ns/op
BenchmarkAnchoredLiteralLongNonMatch-8    	20000000	        85.1 ns/op
BenchmarkAnchoredShortMatch-8             	20000000	       111 ns/op
BenchmarkAnchoredLongMatch-8              	10000000	       217 ns/op
BenchmarkOnePassShortA-8                  	 3000000	       509 ns/op
BenchmarkNotOnePassShortA-8               	 3000000	       464 ns/op
BenchmarkOnePassShortB-8                  	 5000000	       389 ns/op
BenchmarkNotOnePassShortB-8               	 5000000	       331 ns/op
BenchmarkOnePassLongPrefix-8              	20000000	        90.2 ns/op
BenchmarkOnePassLongNotPrefix-8           	 5000000	       302 ns/op
BenchmarkMatchParallelShared-8            	30000000	        59.1 ns/op
BenchmarkMatchParallelCopied-8            	20000000	        60.9 ns/op
BenchmarkQuoteMetaAll-8                   	20000000	        92.3 ns/op	 151.73 MB/s
BenchmarkQuoteMetaNone-8                  	30000000	        48.1 ns/op	 540.88 MB/s
BenchmarkMatch/Easy0/32-8                 	20000000	        58.0 ns/op	 551.51 MB/s
BenchmarkMatch/Easy0/1K-8                 	 5000000	       259 ns/op	3939.41 MB/s
BenchmarkMatch/Easy0/32K-8                	  300000	      4134 ns/op	7924.96 MB/s
BenchmarkMatch/Easy0/1M-8                 	   10000	    220826 ns/op	4748.42 MB/s
BenchmarkMatch/Easy0/32M-8                	     200	   7406666 ns/op	4530.30 MB/s
BenchmarkMatch/Easy0i/32-8                	 2000000	       953 ns/op	  33.58 MB/s
BenchmarkMatch/Easy0i/1K-8                	   50000	     25836 ns/op	  39.63 MB/s
BenchmarkMatch/Easy0i/32K-8               	    2000	   1072221 ns/op	  30.56 MB/s
BenchmarkMatch/Easy0i/1M-8                	      50	  34027283 ns/op	  30.82 MB/s
BenchmarkMatch/Easy0i/32M-8               	       1	1147085221 ns/op	  29.25 MB/s
BenchmarkMatch/Easy1/32-8                 	30000000	        54.7 ns/op	 585.51 MB/s
BenchmarkMatch/Easy1/1K-8                 	 2000000	       718 ns/op	1424.34 MB/s
BenchmarkMatch/Easy1/32K-8                	   50000	     30969 ns/op	1058.09 MB/s
BenchmarkMatch/Easy1/1M-8                 	    2000	   1085243 ns/op	 966.21 MB/s
BenchmarkMatch/Easy1/32M-8                	      50	  34915840 ns/op	 961.01 MB/s
BenchmarkMatch/Medium/32-8                	 2000000	       820 ns/op	  39.01 MB/s
BenchmarkMatch/Medium/1K-8                	   50000	     24286 ns/op	  42.16 MB/s
BenchmarkMatch/Medium/32K-8               	    2000	   1045737 ns/op	  31.33 MB/s
BenchmarkMatch/Medium/1M-8                	      50	  33503487 ns/op	  31.30 MB/s
BenchmarkMatch/Medium/32M-8               	       1	1075915924 ns/op	  31.19 MB/s
BenchmarkMatch/Hard/32-8                  	 1000000	      1251 ns/op	  25.56 MB/s
BenchmarkMatch/Hard/1K-8                  	   50000	     37643 ns/op	  27.20 MB/s
BenchmarkMatch/Hard/32K-8                 	    1000	   1474564 ns/op	  22.22 MB/s
BenchmarkMatch/Hard/1M-8                  	      30	  48328999 ns/op	  21.70 MB/s
BenchmarkMatch/Hard/32M-8                 	       1	1677001522 ns/op	  20.01 MB/s
BenchmarkMatch/Hard1/32-8                 	  200000	      7289 ns/op	   4.39 MB/s
BenchmarkMatch/Hard1/1K-8                 	   10000	    217006 ns/op	   4.72 MB/s
BenchmarkMatch/Hard1/32K-8                	     200	   6988508 ns/op	   4.69 MB/s
BenchmarkMatch/Hard1/1M-8                 	       5	 227978092 ns/op	   4.60 MB/s
BenchmarkMatch/Hard1/32M-8                	       1	7342015123 ns/op	   4.57 MB/s
BenchmarkMatch_onepass_regex/32-8         	 2000000	       707 ns/op	  45.24 MB/s	       0 B/op	       0 allocs/op
BenchmarkMatch_onepass_regex/1K-8         	  100000	     20132 ns/op	  50.86 MB/s	       0 B/op	       0 allocs/op
BenchmarkMatch_onepass_regex/32K-8        	    2000	    642681 ns/op	  50.99 MB/s	       0 B/op	       0 allocs/op
BenchmarkMatch_onepass_regex/1M-8         	     100	  20804244 ns/op	  50.40 MB/s	      11 B/op	       0 allocs/op
BenchmarkMatch_onepass_regex/32M-8        	       2	 660706838 ns/op	  50.79 MB/s	     564 B/op	       1 allocs/op
BenchmarkCompileOnepass-8                 	  300000	      4169 ns/op	    4016 B/op	      52 allocs/op

Usually the stdlib is faster, but not always. If you need the specific backtracking features I have found the performance is acceptable. If you don't use the backtracking features then you should just use regexp from the stdlib.

@dlclark dlclark closed this as completed Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants