Created
July 20, 2015 19:15
-
-
Save PragTob/c843d4f5aca6113d94e9 to your computer and use it in GitHub Desktop.
meta definitions with more work
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 'benchmark/ips' | |
class Try | |
def initialize(array) | |
@array = array | |
end | |
define_method :meta_concat_sort do |array| | |
value = instance_variable_get '@' + :array.to_s | |
new_array = value + array | |
new_array.sort | |
end | |
def concat_sort(array) | |
new_array = @array + array | |
new_array.sort | |
end | |
end | |
BASE_ARRAY = [8, 2, 400, -4, 77] | |
SMALL_INPUT_ARRAY = [1, 88, -7, 2, 133] | |
BIG_INPUT_ARRAY = (1..100).to_a.shuffle | |
def do_benchmark(description, input) | |
puts description | |
Benchmark.ips do |b| | |
try = Try.new BASE_ARRAY | |
b.report('meta_concat_sort') { try.meta_concat_sort(input) } | |
b.report('concat_sort') { try.concat_sort(input) } | |
b.compare! | |
end | |
end | |
do_benchmark('Small input array', SMALL_INPUT_ARRAY) | |
do_benchmark('Big input array', BIG_INPUT_ARRAY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on my machine and with 2.2.2:
And jruby-1.7.21: