Sha256: a0829168b8379e26ce73d997ced09ceace48fd72cc7ade07c12cdc86bf1c8adb

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 KB

Contents

#!/usr/local/bin/ruby -w

begin
  require 'rubygems'
rescue LoadError
  $: << 'lib'
end
require 'inline'

class MyTest

  def factorial(n)
    f = 1
    n.downto(2) { |x| f *= x }
    f
  end

  inline do |builder|
    builder.c "
    long factorial_c(int max) {
      int i=max, result=1;
      while (i >= 2) { result *= i--; }
      return result;
    }"

    builder.c_raw "
    static
    VALUE
    factorial_c_raw(int argc, VALUE *argv, VALUE self) {
      int i=FIX2INT(argv[0]), result=1;
      while (i >= 2) { result *= i--; }
      return INT2NUM(result);
    }"
  end
end

# breakeven for build run vs native doing 5 factorial:
#   on a PIII/750 running FreeBSD:        about 5000
#   on a PPC/G4/800 running Mac OSX 10.2: always faster

require 'benchmark'
puts "RubyInline #{Inline::VERSION}" if $DEBUG

MyTest.send(:alias_method, :factorial_alias, :factorial_c_raw)

t = MyTest.new()
max = (ARGV.shift || 1_000_000).to_i
n   = (ARGV.shift || 5).to_i
m   = t.factorial(n)

def validate(n, m)
  if n != m then raise "#{n} != #{m}"; end
end

puts "# of iterations = #{max}, n = #{n}"
Benchmark::bm(20) do |x|
  x.report("null_time") do
    for i in 0..max do
      # do nothing
    end
  end

  x.report("c") do
    for i in 0..max do
      validate(t.factorial_c(n), m)
    end
  end

  x.report("c-raw") do
    for i in 0..max do
      validate(t.factorial_c_raw(n), m)
    end
  end

  x.report("c-alias") do
    for i in 0..max do
      validate(t.factorial_alias(n), m)
    end
  end

  x.report("pure ruby") do
    for i in 0..max do
      validate(t.factorial(n), m)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
RubyInline-3.8.3 example.rb
RubyInline-3.8.2 example.rb
RubyInline-3.6.7 example.rb
RubyInline-3.6.6 example.rb
RubyInline-3.7.0 example.rb
RubyInline-3.8.0 example.rb
RubyInline-3.8.1 example.rb
RubyInline-3.6.3 example.rb
RubyInline-3.6.4 example.rb
RubyInline-3.6.5 example.rb