我的测试结果:
因为机子太慢我改成5000000了TAT
啥?为啥不一样?因为我改了源码。
class Test def self.test(string,&block) time = Time.new block.call puts string,Time.new-time end end class Point attr_accessor :x, :y def initialize(x, y) @x = x @y = y end def ==(point) return @x==point.x && @y==point.y end end class Array def point_include?(point_arr) self.each do |point| return true if point[0] == point_arr[0] && point[1] == point_arr[1] end return false end end point1 = Array.new point2 = Array.new max = 5000000 Test.test("Push Class Point") { max.times { |i| point1.push Point.new(i,i) } } Test.test("Push Class Array") { max.times { |i| point2.push [i,i] } } Test.test("Include Class Point") { point1.include?(Point.new(max,0)) } # 取最坏情况 Test.test("Include Class Array") { point2.point_include?([max,0]) }
class Test
def self.test(string,&block)
time = Time.new
block.call
puts string,Time.new-time
end
end
class Point
attr_accessor :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(point)
return @x==point.x && @y==point.y
end
end
class Array
def point_include?(point_arr)
self.each do |point|
return true if point[0] == point_arr[0] && point[1] == point_arr[1]
end
return false
end
end
point1 = Array.new
point2 = Array.new
max = 5000000
Test.test("Push Class Point") { max.times { |i| point1.push Point.new(i,i) } }
Test.test("Push Class Array") { max.times { |i| point2.push [i,i] } }
Test.test("Include Class Point") { point1.include?(Point.new(max,0)) } # 取最坏情况
Test.test("Include Class Array") { point2.point_include?([max,0]) }
总觉得Array#==方法太不自然了。 |