Table of Content
A short analysis of different alternatives to check IPv4 validity comparing complexity vs performance of their implementations.
In this post, we will analyze how Go
can be used to squeeze computer performance at maximum, with a tiny example of how to parse and validate content. In this case, we will be using IPv4
detection methods in order to make different approaches, and to benchmark & compare how Go behaves in each of them. We will be testing:
- net package.
- regular expressions.
- custom implementation.
All tests are executed with latest available version of Go at the time of writing this article which is go version go1.14.4 linux/amd64
.
|
|
|
|
Introducing different implementations
Method 1: net
package
This method will use go net
package to check if given string contains a valid IPv4 or not.
|
|
Method 2: regex implementation
This method will use go regex
package to check if given string contains a valid IPv4 or not. The selected regular expression for this purpose is:
|
|
And this is how we use it:
|
|
Note: it is important to compile regex once, and call it multiple times.
Method 3: custom implementation
Our custom approach will be based on net.Parse
method as implemented in standard Go package.
|
|
Benchmarking our implementations
Letβs prepare a simple benchmark tests in where we receive IP value as string and we need to determine if it is a valid IPv4 or not just returning a boolean value with the result.
All test will have following configuration:
- Measure allocations per operation
- Measure ns per operation
- Measure bytes per operation
All benchmarking test will be executed of course, with testing machine in idle state with no other workloads being executed at same time to avoid bias results.
Method 1 Benchmarking: net package
|
|
|
|
Method 2 Benchmarking: regex package
|
|
|
|
Method 3 Benchmarking: custom method
|
|
|
|
Final Results
Once tested and benchmarking all different methods, and having solid results, letβs compare the performance gain between our different options. But first, lets take a look at final results!!
|
|
…and plotting results, we get:
You can check and run performance test by yourself. Get the code at Github
Conclusions
Obviously this optimizations was quite a tiny one, and it was used in order to show you the relationship between readable code and optimized code. Just take into account the huge impact that can have writing good code vs writing code when we scale our software. For a http server, this means that for each validation it will require to make one allocations while the optimized version is zero-alloc! And compared to regex version, the optimized code is x20 times faster. Thanks for checking this out and I hope you found the info useful! This is a basic introduction to how to optimize stuff for performance.
Subscribe, donate or become premium
π¬ Share this post in social media
Thanks for checking this out and I hope you found the info useful! If you have any questions, don't hesitate to write me a comment below. And remember that if you like to see more content on, just let me know it and share this post with your colleges, co-workers, FFF, etc.