Years ago I was writing perl
scripts and regexp became a default string manipulation approach for me. I even had a book dedicated to regexp intricacies.
A decade and few languages later, regexp is still a tempting tool.
Going with go
, one can assume that statically compiled language with regexp matcher pre-compiled, should be blazing fast. Isn’t it?
Lets test. Testing framework is a first class citizen in go
not just for fun, but for profit as well.
Will test on a quite simple schenario of checking if string is empty or end with a question mark. Full code can be found at github.com/ataraskov.
Regexp function to test:
|
|
Strings function to test:
|
|
Benchmark results:
BenchmarkRegexp-16 120072 9792 ns/op 11030 B/op 177 allocs/op
BenchmarkStrings-16 71365939 16 ns/op 0 B/op 0 allocs/op
The winner is obvious. Strings approach is more readable, faster, and cheaper. Though discarding regexp is not an option either. But going with regexp should be a last resort, IMHO ;)