A few days back I wrote about benchmarking your Ruby code, today I want to show you quickly how to profile your code. We have all been there, slow code, select n+1 and all the other craziness that goes on when you write code. You end up putting in code to time blocks of code, or you buy a third-party profiler to tell you where you code is crap.

Ruby has a built-in crap-o-meter, and it is part of the Standard Library so you can use it for free!

As you can see in the code, one little require bringing in the ‘profile’ library, and that is it. Running this code outputs:

Pretty interesting stuff right? All I needed was a require and I get some awesome profiling. You would obviously want to hide this behind a flag of some sorts. Nonetheless awesomeness!

Discussion, links, and tweets

My name is Deon Heyns and I am a developer learning things and documenting them in realtime. Python, Ruby, Scala, .NET, and Groovy are all languages I have written code in. I appeared in the New York Post once. I host my code up at GitHub and Bitbucket so have a look at my code, fork it and send those pull requests.