How it works...

Benchmarking helps us tune applications and do certain micro-optimizations for things such as memory allocations. When benchmarking allocations for applications with input, it's important to try a variety of input sizes to determine whether it affects allocations. We wrote two functions, concat and join. Both join together a variadic string parameter with spaces, so the arguments (a, b, c) will return the string a b c.

The concat approach accomplishes this solely through string concatenation. We create a string and append the strings in the list and spaces in a for loop. We omit adding a space on the last loop. The join function uses the internal Strings.Join function to accomplish this far more efficiently in most cases. It can be helpful to benchmark standard library compared to your own functions to help better understand trade-offs in performance, simplicity, and functionality.

We used sub-benchmarks to test all of our parameters, which also work excellently with table-driven benchmarks. We can see how concat approach results in a lot more allocations than join, at least for single length inputs. A good exercise would be to try this with variable-length input strings as well as a number of arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.6