PowerShell benchmarking module built around Measure-Command

Quite often I've found myself wondering what will work faster, given two or more ways of solving a problem. A way to get a clue is to benchmark it. This will allow you to optimize scripts in some cases. Presented here is a PowerShell module called Benchmark which exports the function Measure-These, which will work like a cmdlet, and can be used to measure how long one or more script blocks take to execute the specified number of times.

I use Measure-Object to collect data about the processed script blocks, and apparently there is quite a bit of overhead involved in this, so if the measured commands take about 1.1 seconds with 10,000 iterations, you might find the benchmark script itself to take maybe 12 seconds, as in one of my test cases. The overhead seems to increase fast with the number of iterations. I tried rolling my own algorithm instead of using Measure-Object, but failed miserably at improving the performance.

If you prefer to have it as a function in your profile rather than a module, simply copy and paste the entire content of the .psm1 file into your PowerShell profile (external Microsoft site link). You can also dot-source the script file containing the Measure-These function on demand when you need it, but you need to rename the file to have a .ps1 extension as modules pop up in notepad (for "security" reasons) when dot-sourced:

Compare some methods of determining if a string is contained within another string. 50,000 iterations. Odd (apparently very, very low) minimum numbers in this case. I haven't seen this happen again over the next dozens of times I've used the module.