Timing Ruby Code – It Is Easy With Benchmark

Recently I needed to time some Ruby code as it was running (related to my post on list intersection and another post on skip pointers which I haven’t finished writing yet). Being a bit too keen (as I often am :)) and seeing as it would be fairly easy to do, I decided to quickly roll an implementation similar to the following.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
beginning_time = Time.now
(1..10000).each { |i| i }
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"```

You get the current time before and the current time after the code you need to measure the running time of and you simply subtract them from each other (_to get the time in seconds_). The output is as follows:

<pre>[email protected]:~/projects$ ruby time.rb
Time elapsed 4.049 milliseconds</pre>

But this seemed rather inflexible, and you wouldn't want to have to do it over and over again, so I rolled it into a method, to allow myself to time the execution of other methods, like so.

```ruby
def time_method(method, *args)
  beginning_time = Time.now
  self.send(method, args)
  end_time = Time.now
  puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
end

def method_to_time(*args)
  (1..10000).each { |i| i }
end

time_method(:method_to_time)```

As you can see we can now pass a symbol version of the method name we want to time into our _time_method_. Inside the _time_method_ we will call "_send_" on the current object passing in the method name (_which will essentially call the method we want to time_) and will wrap this call in our timing code, producing similar output (_to that from above_):

<pre>[email protected]:~/projects$ ruby time.rb
Time elapsed 6.198 milliseconds</pre>

This is somewhat better, but now we can only time methods, not arbitrary code which is not very nice. So, we enhance our _time_method_ by allowing it to take a block.

```ruby
def time_method(method=nil, *args)
  beginning_time = Time.now
  if block_given?
    yield
  else
    self.send(method, args)
  end
  end_time = Time.now
  puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
end

time_method do
  (1..10000).each { |i| i }
end
[email protected]:~/projects$ ruby time.rb
Time elapsed 6.198 milliseconds

This allows us to time arbitrary code as well as methods – awesome!

However as I was browsing some code the other day (for unrelated reasons), I discovered that it wasn’t so awesome, because I reinvented the wheel and the existing wheel is potentially better.

If enjoy my Ruby posts (like this one), here is some of the Ruby related stuff I am planning to write about in the near future.

  • Passing methods as arguments in Ruby
  • Ruby object serialization (and deserialization :))
  • A look at serializing Ruby blocks/procs
  • plus much more…

Be sure to subscribe to my feed if you don’t want to miss it.

Ruby Benchmark Module

Ruby already has a module as part of its standard libraries that basically does all that timing stuff I was playing with above, it’s the Benchmark module. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
require "benchmark"

time = Benchmark.measure do
  (1..10000).each { |i| i }
end
puts time```

As you can see we pass the block we want to time to _Benchmark.measure_ which will return output that will look like this when printed:

<pre>[email protected]:~/projects$ ruby time.rb
  0.010000   0.000000   0.010000 (  0.003298)</pre>

You're probably getting the picture already, but if not here is another way to use benchmark which will label our output to make it clearer:

```ruby
require "benchmark"

Benchmark.bm(7) do |x|
  x.report("first:")   { (1..10000).each { |i| i } }
  x.report("second:") { (1..10000).each { |i| i }}
  x.report("third:")  { (1..10000).each { |i| i }}
end
[email protected]:~/projects$ ruby time.rb
             user     system      total        real
first:   0.000000   0.010000   0.010000 (  0.008010)
second:  0.000000   0.000000   0.000000 (  0.005251)
third:   0.000000   0.000000   0.000000 (  0.003678)

Not only did we run and time our code, but we did it multiple times producing, the user, system, total and real times for each run. You can imagine how useful this could be especially considering that if we use one of the other methods of the Benchmark module (Benchmark.benchmark) it will allow us to format our output as well as easily produce averages and totals for all the runs.

Of course if you don’t want all that extraneous stuff printed, the easiest way to use it is:

1
2
3
4
5
6
require "benchmark"

time = Benchmark.realtime do
  (1..10000).each { |i| i }
end
puts "Time elapsed #{time*1000} milliseconds"
[email protected]:~/projects$ ruby time.rb
Time elapsed 5.35893440246582 milliseconds

Which simply gives you the real elapsed time in seconds which you can then print out yourself. In hindsight I am still pretty happy with my own _timemethod way as it is simple, flexible and mine :). But it is good to know that Ruby already provides a facility to easily time execution without having to write any extra code.

Image by monkeyc.net

The Difference Between A Developer, A Programmer And A Computer Scientist

I have often used those three terms almost interchangeably, yes, even computer scientist. After all, most of us have a degree in computer science, so what does that make us? However, recently I find that those three things have come to take on more and more distinct personalities in my mind. It has come to the point where if I think about someone I know – or know of – within the industry, they immediately fall into one of those three categories. Which is not to say that one person can’t have attributes from all three, but regardless, they always tend to favor one most strongly and so I fit them into that category, programmer, developer or computer scientist.

It is difficult to define what each one should be, (it is more of a gut feel rather than a strict delineation) they are very similar (and rightly so), but I am going to attempt to do it anyway, cause I am a glutton for punishment :).

Computer Scientist

They write code (yeah I know it’s a bit of a bombshell). It may not be the prettiest or most well-factored code, but it gets the job done. It is not about the design of the code or “good” practices, it is about proving what they set out to prove. A computer scientist is as much a mathematician as they are a technologist (they have 31337 math skills), they don’t just need to know that stuff works, they have to prove it. Communication and people skills are desirable traits, but not emphasized. Software process and team dynamics skills are desirable traits, but not emphasized. They have good breadth of general knowledge of their whole field, but they deeply specialize in one or several narrow areas. In these areas they are considered world-class experts. They work on stuff related to their research in their personal time.

Programmer

Programmers write awesome code. Making it clean, well-factored and error free are very important concerns, but not at the expense of getting the job done. It is all about knowing the meaning of “good code” within their domain. They need to have some math skills, but this is not a paramount concern. They need to know of good (best) solutions to problems, but they don’t need to prove it is the best solution. A good breadth of algorithmic knowledge is imperative. They have a depth of skill in a wide area of expertise and have reasonably good knowledge of related areas as well. Communication and people skills are desirable traits, but not emphasized. Software process and team dynamics skills are desirable traits, but not emphasized. They work on personal software projects they find of interest in their off time.

Developer

They write code. Making it well-factored and clean is important, but other factors often take priority. Math skills are very much optional, but it does help to be aware of common problems and solutions related to the domain they are in. Communication and people skills are paramount. Process and team dynamics are bread and butter skills. They are consummate generalists without any truly deep specializations. They are expert at finding ways around problems and plugging components together to fulfill a set of requirements. In their personal time they are either trying to build the next Facebook, or engage in activities that have nothing to do with programming, developing, or computer science.

  • _Developer are programmers to a greater or lesser extent.

_ * Computer scientists are programmers to a greater or lesser extent.

  • _Enterprise software is the domain of the developer.

_ * _The Googles and Microsofts of the world are after programmers (and to a lesser extent computer scientists). The developers who end up there become product managers.

_ * RnD and academia are the domain of the computer scientist (and to a lesser extent the programmer)

The thing to remember here is that none of the three is derogatory or “bad” in any way. One is not more or less desirable than any of the others. They are simply different dimensions (with some crossover) of the field we are all involved in. Particular personalities will identify more with one but that does not mean that all three can’t “bleed” into each other and combine favorably. It is entirely possible to be both an awesome developer and a great programmer (although it is difficult with so many important things to focus on). In rare cases you may even get an all 3 in 1 type of deal, in which case I’d love to hear from you, cause we should start a company together, so that I can ride your awesomeness all the way to easy-street :). But no matter where you fall, it is entirely possible to be highly successful if you fit snugly into just one of the three.

What about a software engineer? That’s just a subset of developer.

What about an architect? They design buildings and stuff, so I am not quite sure how that’s relevant :)

I do believe that I have thoroughly failed to communicate my meaning. No matter. I will throw the ball to you, dear reader. Do you see programmer, developer and computer scientist as distinct and if so are you definitions similar to mine? If not, then I’d love to hear your thoughts about them being one and the same.

Images by Esthr, Chealion and Pieter Baert

How To Fix The WP-Syntax Special Character Escaping Issue

I use WordPress as my blogging platform, it’s a great tool, has a heap of plugins so you can do much with very little effort, which suits me fine :). As a software development blog I often have code snippets that I need to post and I want these to look good (i.e. formatting, syntax highlighting). This is why I use the WP-Syntax WordPress plugin. I am not going to go into why it’s so great, if you’re curious you can check it out for yourself. Suffice to say, it does the job fine except for one very annoying issue. Whenever you have any kind of special characters in your code (which you inevitably do e.g. <, >, & etc.), these always render as their escaped representations. For example if you wanted to write:

if (foo < 5 && bar > 6)

you would end up with:

if (foo < 5 && bar > 6)

Extremely annoying. This only happens if you use the WYSIWYG editor in WordPress, which is why the plugin FAQ recommends switching off the visual editor and using the source code editor, but that eventually gets to be beyond annoying.

The other day my annoyance threshold reached it’s limit and I decided to find a solution to this issue. I armed myself with an apple (for munching purposes :)) and set to googling. I eventually found a couple of sites which mentioned this issue e.g. this one and this one. Both of these tried to point me to the following link – http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html.

Unfortunately when I tried to go there I got:

Parse error: syntax error, unexpected $end in /home/felho/www/blog.felho.hu/wp-includes/default-filters.php  on line 213

What a massive fail! Most of the time you don’t want to duplicate information that can be found in other places on the web (which is why we link), but sometimes the beauty of the internet is the fact that you can find the same info in many different places. In this case – the internet failed. The information was only to be found in one place (with everyone else linking there) and that place was dead.

Thankfully the magic of google cache came to the rescue and I was able to recover the original post. So, in the interest keeping the information alive on the web :), I’ll re-post the solution here. Here is what you need to do to fix that special character escaping issue:

  • Jump into your WordPress configuration and deactivate the WP-syntax plugin for the moment
  • Jump into your WP-syntax plugin folder, in my case it was the wp-content/plugins/wp-syntax folder under the root of your WordPress install
  • Crack open the wp-syntax.php file and find the wp_syntax_highlight function (in my case it was on line 94)
  • Inside the function find the following line:
$geshi = new GeSHi($code, $language);

Replace this line with the following:

$geshi = new GeSHi(htmlspecialchars_decode($code), $language);

If you’re running your WordPress install on a server with a PHP version which is older than 5.1, you will not actually have the _htmlspecialcharsdecode() function. Of course I would be surprised if your PHP version was older than 5.1, but just in case it is, here is what you need to do. You will need to roll your own _htmlspecialcharsdecode() function, so put in the following code right before the line that you replaced above:

if (!function_exists("htmlspecialchars_decode")) {
    function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) {
        return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
    }
}
  • Save and exit the file and then go an reactive your plugin in the WordPress config.

This should solve your special character escaping issue – it solved mine.

Here is the funny thing, after I made sure everything was working I went and had a look at the code in wp-syntax.php again, and here is what I found right above the line that we replaced:

if ($escaped == "true") $code = htmlspecialchars_decode($code);

Now, I am no PHP expert, but it looks like we shouldn’t have needed to do what we did, this line should have taken care of everything, but obviously $escaped isn’t equal to “true” and so _htmlspecialchars_decode()_ function never gets called (either that or it does get called, but shouldn’t be). I didn’t really want to experiment with my live blog, but if you have a extraneous WordPress install with WP-syntax floating around somewhere feel free to poke around. It feels like this shouldn’t be an issue. Anyways, do let me know if you find something out. In the meantime you have a work-around thanks to blog.felho.hu (which is still down :)) and google cache.   

Image by Dr Case