Skip to content

Instantly share code, notes, and snippets.

@manveru
Created September 20, 2010 13:33
Show Gist options
  • Save manveru/587910 to your computer and use it in GitHub Desktop.
Save manveru/587910 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Numeric
def split_digits_1(base = 10)
# head, last = self.divmod(base)
head = self / base
last = self % base
head < base ? [head, last] : head.split_digits_1(base).push(last)
end
def split_digits_2(base = 10)
total, result = self, []
while total > 0
fraction = total % base
total /= base
result << fraction
end
result.reverse
end
def split_digits_3(base = 10)
total, result = self, []
while total > 0
fraction = total % base
total /= base
result.unshift fraction
end
result
end
def split_digits_4(base = 10)
total, result = self, []
while total > 0
total, fraction = total.divmod(base)
result << fraction
end
result.reverse
end
end
N = 100e100.to_i
TIMES = 10_000
p N.split_digits_1
p N.split_digits_2
p N.split_digits_3
p N.split_digits_4
Benchmark.bmbm do |b|
b.report '1' do
TIMES.times do
N.split_digits_1
end
end
b.report '2' do
TIMES.times do
N.split_digits_2
end
end
b.report '3' do
TIMES.times do
N.split_digits_3
end
end
b.report '4' do
TIMES.times do
N.split_digits_3
end
end
end
@manveru
Copy link
Author

manveru commented Sep 20, 2010

iota ~prog/ruby/bench % ruby split_digits.rb
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 0, 4, 9, 5, 1, 3, 2, 6, 5, 2, 4, 5, 3, 3, 6, 6, 2, 8, 4, 4, 6, 8, 4, 2, 7, 1, 9, 9, 2, 4, 1, 5, 0, 0, 0, 6, 1, 2, 9, 9, 9, 5, 9, 7, 4, 7, 3, 1, 9, 9, 3, 4, 5, 2, 1, 8, 0, 7, 8, 9, 9, 1, 1, 3, 0, 3, 2, 6, 1, 2, 9, 4, 4, 8, 1, 5, 1, 1, 5, 4, 6, 8, 8, 0]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 0, 4, 9, 5, 1, 3, 2, 6, 5, 2, 4, 5, 3, 3, 6, 6, 2, 8, 4, 4, 6, 8, 4, 2, 7, 1, 9, 9, 2, 4, 1, 5, 0, 0, 0, 6, 1, 2, 9, 9, 9, 5, 9, 7, 4, 7, 3, 1, 9, 9, 3, 4, 5, 2, 1, 8, 0, 7, 8, 9, 9, 1, 1, 3, 0, 3, 2, 6, 1, 2, 9, 4, 4, 8, 1, 5, 1, 1, 5, 4, 6, 8, 8, 0]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 0, 4, 9, 5, 1, 3, 2, 6, 5, 2, 4, 5, 3, 3, 6, 6, 2, 8, 4, 4, 6, 8, 4, 2, 7, 1, 9, 9, 2, 4, 1, 5, 0, 0, 0, 6, 1, 2, 9, 9, 9, 5, 9, 7, 4, 7, 3, 1, 9, 9, 3, 4, 5, 2, 1, 8, 0, 7, 8, 9, 9, 1, 1, 3, 0, 3, 2, 6, 1, 2, 9, 4, 4, 8, 1, 5, 1, 1, 5, 4, 6, 8, 8, 0]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 0, 4, 9, 5, 1, 3, 2, 6, 5, 2, 4, 5, 3, 3, 6, 6, 2, 8, 4, 4, 6, 8, 4, 2, 7, 1, 9, 9, 2, 4, 1, 5, 0, 0, 0, 6, 1, 2, 9, 9, 9, 5, 9, 7, 4, 7, 3, 1, 9, 9, 3, 4, 5, 2, 1, 8, 0, 7, 8, 9, 9, 1, 1, 3, 0, 3, 2, 6, 1, 2, 9, 4, 4, 8, 1, 5, 1, 1, 5, 4, 6, 8, 8, 0]
Rehearsal -------------------------------------
1   1.170000   0.000000   1.170000 (  1.184450)
2   1.140000   0.000000   1.140000 (  1.139748)
3   1.190000   0.000000   1.190000 (  1.193600)
4   1.190000   0.010000   1.200000 (  1.198022)
---------------------------- total: 4.700000sec

        user     system      total        real
1   1.180000   0.000000   1.180000 (  1.178914)
2   1.150000   0.000000   1.150000 (  1.149533)
3   1.190000   0.000000   1.190000 (  1.191845)
4   1.190000   0.000000   1.190000 (  1.199869)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment