Skip to content

Instantly share code, notes, and snippets.

@reu
Created April 16, 2012 17:00
Show Gist options
  • Save reu/2399965 to your computer and use it in GitHub Desktop.
Save reu/2399965 to your computer and use it in GitHub Desktop.
NUM2DBL slow BigDecimal convertion
# Passos para rodar:
# 1. ruby extconf.rb
# 2. make
# 3. ruby benchmark.rb
require 'benchmark'
require 'bigdecimal'
require './test'
n = 50
decimals = (1..10_000).map { |value| BigDecimal.new [value, (rand * 10000000000).to_i].join(".") }
floats = (1..10_000).map(&:to_f)
Benchmark.bm do |b|
b.report do
n.times { decimals.each(&:convert) }
end
b.report do
n.times { floats.each(&:convert) }
end
end
# Resultados Mac 2.4ghz
# user system total real
# 2.790000 0.010000 2.800000 ( 2.824529)
# 0.030000 0.000000 0.030000 ( 0.038669)
require "mkmf"
create_makefile("test")
#include <ruby.h>
static VALUE convert(VALUE self) {
NUM2DBL(self);
return self;
}
void Init_test() {
rb_define_method(rb_cNumeric, "convert", convert, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment