Unique Marketing, Guaranteed Results.

How to do Benchmarking with Ruby

May 21st, 2009 by Alan Carl Mitchell

Benchmarking with Ruby is super easy. There is already a built in class–Benchmark–that will do all of the heavy lifting for you.

If you want to do basic benchmarking, Benchmark.bm is the easiest way to go. Take a look below. Here we are going to test three ways to do looping in Ruby and see which one we like best.

require 'benchmark'
 
n = 5000000
 
Benchmark.bm do |x|
  x.report("for loop:")   { for i in 1..n; a = "1"; end }
  x.report("times:")      { n.times do   ; a = "1"; end }
  x.report("upto:")       { 1.upto(n) do ; a = "1"; end }
end

This will produce output something like this:

               user     system      total        real
for loop:  0.727000   0.000000   0.727000 (  0.727030)
times:     0.571000   0.000000   0.571000 (  0.571588)
upto:      0.523000   0.000000   0.523000 (  0.522947)

If we use Benchmark.bmbm, then it will do a ‘rehearsal’ run first in order to better equalize the garbage collection environment of the code inside the block so that hopefully we get more realistic timings.

require 'benchmark'
 
n = 5000000
 
Benchmark.bmbm do |x|
  x.report("for loop:")   { for i in 1..n; a = "1"; end }
  x.report("times:")      { n.times do   ; a = "1"; end }
  x.report("upto:")       { 1.upto(n) do ; a = "1"; end }
end

This will produce output something like this:

Rehearsal ---------------------------------------------
for loop:   0.756000   0.000000   0.756000 (  0.756298)
times:      0.477000   0.000000   0.477000 (  0.476855)
upto:       0.527000   0.000000   0.527000 (  0.526522)
------------------------------------ total: 1.760000sec

                user     system      total        real
for loop:   0.751000   0.000000   0.751000 (  0.751067)
times:      0.516000   0.000000   0.516000 (  0.515874)
upto:       0.581000   0.000000   0.581000 (  0.581254)

Alternately, you can use the Benchmark.realtime method like this:

puts "for loop: #{Benchmark.realtime {for i in 1..n ; a = "1" ; end}}"

and get something like this:

for loop: 0.682438850402832

Ruby newbie iteration musings

May 5th, 2009 by hals

First off I must note how nice it is that semicolons, and several other punctuation items, are optional.

Another nice thing I have been introduced to is the iterator. In most cases it completely replaces the old for loop. 

var.each {|x| ….}     

or

for x in var {……}

seem much cleaner and easier to write than

for (int i=0; i<10; i++) {…; …;}

Extending this makes it handy to work with multiple parameters. Let’s say that you want to pass in a variable number of arguments, that can also be hashes.

One way of working with them in the method would be as follows:

class Ema

def initialize(p1, p2, *p3)  # the ‘*’ will push arguments 3…n into p3 as an array

# then you might access parameter3 with one of the following .each variants:

def showMe

    @p3.each do |s|    #this gives you each element of the array - 

      puts “  p3: s = #{s}”

        s.each_key do |y|     #each element is a hash, so this gives you the keys

          puts ”     #{y} = #{s[y]}”

        end

        s.each do |y,z|       #this will give you the key, value pairs

          puts ”     #{y} = #{z}”

        end

        s.each do |r|        #and this gives you the key,value in an array

          puts ”     #{r[0]} = #{r[1]}”

        end

    end

end

 a = Ema.new(“one”, “two”, {“apple” => 3}, {“pear” => 1}, {“grape” => 5}, {“kiwi” => 33})

a.showMe

Ruby on Rails Spotlight Search in Mac OS Leopard

April 15th, 2009 by Chuck Wood

Have you ever wished you could search the Ruby on Rails API with Spotlight? It turns out that you can!

Mac OS X Leopard’s Spotlight search will search through dictionaries you install on your machine and enable. And, it just turns out that such a dictionary exists for Ruby on Rails written by Priit Haamer. Here’s how to set it up:

Read the rest of this entry »

Ruby Chop vs Chomp

April 14th, 2009 by jaredd

When you receive user input from textareas or console input you may get some newline characters. One way to remove newline characters is the String#chop method, it will remove any trailing newline or carriage return characters “\r\n”. But it’s tricky. Because here it works like it’s suppose to.

full_name = "My Name is Jared\r\n"  
full_name.chop! # => "My Name is Jared"

Now if you run chop and there are no newline characters.

puts full_name    #=> "My Name is Jared"
full_name.chop!  #=> "My Name is Jare"

Disaster Strikes!

That’s why there is chomp.

puts full_name       #=> "My Name is Jared\r\n"
full_name.chomp!  #=> "My Name is Jared"
full_name.chomp!  #=> "My Name is Jared"

So chomp is our recommended way of removing trailing newline characters.

Interactive Ruby With Tab Completion

April 10th, 2009 by Aaron Murphy

Do you like tab completion with bash? Now you can have it with irb too! Just setup your .bashrc file with an alias so you don’t have to remember the entire command line. It’s not exactly the same as bash. You will have to hit the tab key twice. On my Ubuntu 8.10 system it works as listed below.

Read the rest of this entry »

Tutorial: How to install/setup Cucumber

April 9th, 2009 by Alan Carl Mitchell

This tutorial assumes that you have ruby, rails, and mysql installed on your machine, and doesn’t explain too much about Cucumber and will mostly show you a quick way to get it working. A lot of material is borrowed from this tutorial on setting up RSpec and Factory Girl. It may be useful to look over first, but not necessary.

Read the rest of this entry »

Tutorial: How to install/setup RSpec (and Factory Girl) in Rails

April 3rd, 2009 by Alan Carl Mitchell

This tutorial assumes that you have ruby, rails, and mysql installed on your machine. This tutorial also doesn’t explain much about RSpec or Factory Girl and will just show you a quick way to get them working in a RoR environment.

Read the rest of this entry »

Using Ruby Unpack To Save Strings To Integers

April 3rd, 2009 by Aaron Murphy

What does unpack do? It’s used to take a previously packed string of usually binary data and unpack into it’s original binary data. Pack is a method of Array which returns a string. But I wanted to use it to store data in a set of integers.

Read the rest of this entry »

Foreign Key Migrations Plugin

April 2nd, 2009 by Chuck Wood

I worked on a plugin yesterday that would include all of our migration helpers. You can find it on github. Currently, it only does foreign keys work, but eventually, I’d like to have it add unique indexes on columns defined with a :unique => true attribute in the ActiveRecord migration. But for right now, here’s how to use it:

Adding a foreign key that references the id column in another table:

# The column 'id' is assumed in the referenced table.
add_foreign_key :table1, :table2_ref, :table2

Read the rest of this entry »


Copyright © 2005-2011 PMA Media Group. All Rights Reserved