Benchmarking Scripts: Strings

The latest build of PrimalScript 2011 has a new little toy built in. If you run a script from within PrimalScript, it will time the execution and print the elapsed time after the script has finished in the output window. While this isn’t exactly a profiler of any kind, it can be helpful to evaluate the time it takes to accomplish come common tasks.

For example, let’s look at concatenating strings. Almost every example script we see from users adds strings up to form reports, log files, CSV files and so forth. It’s also not language specific, as we see this type of task in almost every language.

So in order to test this timing output I wrote some sample scripts:

stringbenchmark1.vbs

As you can see, this is a fairly simple sample of adding up strings. I avoid any loading from files or output so we can just see the string performance. On my machine this little script takes about 25 seconds to run. The scripts are all attached as a zip file, so you can test them yourself.

I was curious how PowerShell would do with that:

stringbenchmark1.ps1

This took about 49 seconds to run. Quite a bit slower than VBScript, which isn’t all that surprising as PowerShell uses System.String and that wasn’t meant to be used that way. Of course, if you paid any attention in your PowerShell classes you would know that you are supposed to use the System.Text.StringBuilder class. As shown below:

 

stringbenchmark2.ps1

Now that only takes about a second for the same task.

What about VBScript though? Can we make that  faster? Of course! You can use the same StringBuilder class here:

StringBenchmark2.vbs

And that takes also only about a second to run.

So to sum it up, here are the results again:

Language Without StringBuilder With StringBuilder
VBScript 25 seconds 1 second
PowerShell 49 seconds 1 second

 

Which benchmarks would you like to see?

Download the individual scripts here: stringbenchmarks