I learnt something new in the last couple of days!
Well, technically this is no big news, because I learn something new everyday at the moment.
But this time around I learnt something that I never had to worry about in the past — performance of the code and how to go about improving it.
The reason why I never had to even worry about optimisation is because I had more pertinent concerns: TDD, Object Oriented design, TDD, SOLID principles, TDD, patterns, vim and TDD, among numerous other things. So having to think about improving performance actually makes me feel very grown up!
So before you can begin optimising your code for better performance you need to know which parts are the slow ones, ie where are the bottlenecks, guessing doesn’t work… you need solid metrics and trial/error approach doesn’t cut it, no, zilch, nada… don’t even think about it.
To get the metrics you need to profile the code in question. I am using ruby-prof at the moment, this is my first ever profiling tool and so far I really like it, HTML graph is very useful.
In short to use profiler you include it in the project and then wrap the code you want to test and run the program itself as usual.
What you get from ruby-prof is a break down of all the calls the code under tests is making and how much time each of those calls takes. This allows you to trace down the bottleneck.
Profiling is the initial step you do and its the easiest one. Once you found the bottleneck that’s when the interesting bit begins. You need to make your code faster… and I thought I was already doing my best when writing it, now I have to un-think what I thought previously and solve it differently.
I found this to be the most challenging and the most exciting bit. There’s a lot of room for improvement, and having metrics to guide you is very reassuring, I can make a change and run profiler, which can tell me if I made a step forward or not.
I would strongly recommend everyone to play around with a profiling tool of choice. Seeing results and applying them really makes you question your logic — and this is what I love about programming the most!