Created
April 16, 2012 17:00
-
-
Save reu/2399965 to your computer and use it in GitHub Desktop.
NUM2DBL slow BigDecimal convertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "mkmf" | |
create_makefile("test") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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