Benchmark inline c : Benchmark « Development « Ruby






Benchmark inline c


require 'rubygems'
require 'inline'
require 'benchmark'

class CFactorial
  class << self
    inline do |builder|
      builder.c %q{
        long factorial(int value) {
          long result = 1, i = 1;
          for (i = 1; i <= value; i++) {
            result *= i;
          }
          return result;
        }
      }

    end
  end
end

class Fixnum
  def factorial
    (1..self).inject { |a, b| a * b }
  end
end

Benchmark.bm do |bm|
  bm.report('ruby:') do
    100000.times { 8.factorial }
  end

  bm.report('c:') do
    100000.times { CFactorial.factorial(8) }
  end
end

 








Related examples in the same category

1.Simple Benchmarking
2.Because measure accepts code blocks, you can make it as elaborate
3.Benchmark includes a way to make completing multiple tests more convenient.
4.Benchmark reflection