Benchmark class, what do I need?

Although micro-optimizing don’t give the biggest performance improvement, I often find my self wondering if using function X() instead of Y() is faster, and if my own function Z() is better. Most of the time I end up writing a small tiny benchmark test on the two or three functions, which then gives me a quick overview of the functions speed.
So I’ve decided to make a small and simple to use micro-benchmarking class.


Basically this class is all about being easy to use, so you don’t have to spend a long time setting up a test.

  • Easy to set up. The class should be used for those small tests, where you’re in doubt of which piece of code is fastest. And if one is to test a piece of code, for a tiny performance increase (maybe more out of curiosity than an actual application improvement), it should only take a couple of minutes to set the test up. If it takes 15 minutes to create a test every time, you wont take the time to write the test if you’re in the middle of something else.
  • Readable output. For the same reasons it should be easy to set up, the output needs to be readable when the test is done, without any post-test-processing. It would be good if the output could be chosen, so you could either get the easy to read, or a more in-depth detailed result (maybe formatted for post-processing in another environment).

Nice to have

Again with the intentions of making this easier to work with, it would be great to have a feature, where you would just write the piece of code you wanted to test, and then have the class handle the rest. I’ve been brainstorming a few ways to do this.

  • Functions. Assign a function, the class can call when doing the test. Perhaps it should be possible to assign a set of functions, so you had an init, bench and close function. This would make the testing process easy, write the functions, and then get the result, and even though calling a function will have an impact it should be the same for every test case.
  • Eval the code. This crossed my mind, but I think it’s generally bad practice to use the eval function if you can avoid it. And the code written has to be a string, which will mess up the syntax highlighting. And I don’t really know the overhead of using eval.
  • External files. I could make the class read an external file, which is setup in a way the class would understand. Although this would just be separating the tests in different files, it could have some advantages when doing larger tests. And that is the problem with this solution, I don’t want to do larger tests with this class.

Another thing that would be nice to have, would be an external interface. Mainly a tiny app that could read the output from the class (XML perhaps). Have the class automatically send the output to the app, which then stores it and gives you a quick overview of previous tests as well.
But these are all just nice to have, so far I’ll aim for the goal when I get some free time.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>