Measure performance changes with benchcmp
go test -bench=.
Go has a great option to write your benchmarks and run it together with go test with option -bench. To create a benchmark function you must do the following:
|
|
Anonymizer() function searchs for emails in the string and replaces it to “****”.
|
|
Improve Anonymizer()
Let me show you this Anonymizer():
|
|
Then I decided that this RegExp is a bit complicated and replaced it to the one sufficient for all practical purposes:
|
|
And run go test again:
|
|
Measure our improvement
So now we have 2 versions of our code and benchmark results for both, lets use benchcmp tool to measure a growth of performance:
|
|
Negative values are good, it means that 1 operation calculates faster after our changes. Of cause this benchmark is artificial and values can be different even on the same environment, so don’t forget it and repeat your experiments.