This file contains 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 'ruby-prof' | |
class Array | |
def a_slice_exists? ary | |
raise ArgumentError unless Array === ary | |
each_index.select {|i| at(i) == ary[0]}.any? {|i| self[i,ary.length] == ary} | |
end | |
def b_slice_exists? slice | |
start = slice.first | |
len = slice.size | |
each_with_index do |e, i| | |
return true if e == start && self[i,len] == slice | |
end | |
false | |
end | |
def c_slice_exists? slice | |
each_cons(slice.length).any? {|ary| ary == slice} | |
end | |
end | |
A = [1,2,3,4,5] | |
B = [2,3,4] | |
RubyProf.start | |
1000.times { A.a_slice_exists? B } | |
result = RubyProf.stop | |
printer = RubyProf::FlatPrinter.new(result) | |
printer.print(STDOUT) | |
puts "------" | |
RubyProf.start | |
1000.times { A.b_slice_exists? B } | |
result = RubyProf.stop | |
printer = RubyProf::FlatPrinter.new(result) | |
printer.print(STDOUT) | |
puts "------" | |
RubyProf.start | |
1000.times { A.c_slice_exists? B } | |
result = RubyProf.stop | |
printer = RubyProf::FlatPrinter.new(result) | |
printer.print(STDOUT) |
This file contains 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
Thread ID: 70125953113060 | |
Fiber ID: 70125957549420 | |
Total: 0.024274 | |
Sort by: self_time | |
%self total self wait child calls name | |
32.51 0.011 0.008 0.000 0.003 2000 Array#each_index | |
15.46 0.023 0.004 0.000 0.019 1000 Array#a_slice_exists? | |
11.14 0.003 0.003 0.000 0.000 5000 Array#at | |
10.34 0.004 0.003 0.000 0.002 1000 Array#each | |
5.73 0.012 0.001 0.000 0.011 1000 Enumerable#select | |
5.33 0.005 0.001 0.000 0.004 1000 Enumerable#any? | |
5.27 0.024 0.001 0.000 0.023 1 Integer#times | |
5.10 0.011 0.001 0.000 0.010 1000 Enumerator#each | |
3.57 0.001 0.001 0.000 0.000 1000 Array#== | |
3.06 0.001 0.001 0.000 0.000 1000 Array#[] | |
2.38 0.001 0.001 0.000 0.000 1000 Module#=== | |
0.11 0.024 0.000 0.000 0.024 1 Global#[No method] | |
* indicates recursively called methods | |
------ | |
Thread ID: 70125953113060 | |
Fiber ID: 70125957549420 | |
Total: 0.011421 | |
Sort by: self_time | |
%self total self wait child calls name | |
28.58 0.005 0.003 0.000 0.002 1000 Array#each | |
27.21 0.010 0.003 0.000 0.007 1000 Array#b_slice_exists? | |
13.93 0.006 0.002 0.000 0.005 1000 Enumerable#each_with_index | |
10.87 0.011 0.001 0.000 0.010 1 Integer#times | |
7.43 0.001 0.001 0.000 0.000 1000 Array#== | |
6.67 0.001 0.001 0.000 0.000 1000 Array#[] | |
5.20 0.001 0.001 0.000 0.000 1000 Array#first | |
0.10 0.011 0.000 0.000 0.011 1 Global#[No method] | |
* indicates recursively called methods | |
------ | |
Thread ID: 70125953113060 | |
Fiber ID: 70125957549420 | |
Total: 0.015243 | |
Sort by: self_time | |
%self total self wait child calls name | |
24.44 0.007 0.004 0.000 0.003 1000 Array#each | |
17.00 0.009 0.003 0.000 0.007 2000 Enumerable#each_cons | |
15.06 0.003 0.002 0.000 0.001 2000 Array#== | |
14.70 0.014 0.002 0.000 0.012 1000 Array#c_slice_exists? | |
8.39 0.011 0.001 0.000 0.009 1000 Enumerable#any? | |
8.16 0.015 0.001 0.000 0.014 1 Integer#times | |
7.87 0.009 0.001 0.000 0.008 1000 Enumerator#each | |
4.30 0.001 0.001 0.000 0.000 1000 Fixnum#== | |
0.08 0.015 0.000 0.000 0.015 1 Global#[No method] | |
* indicates recursively called methods |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment