performance
method argument
def hello(foo: nil, bar: nil)
end
10_000.times { hello(foo: 1, bar: 2) }
# Runtime => 10.354 msdef hello(options = {})
foo, bar = options[:foo], options[:bar]
end
10_000.times { hello(foo: 1, bar: 2) }
# Runtime => 5.064 msobj.method VS obj attr_reader
```ruby class Foo def initialize(val) @val = val end
def val @val end end
object = Foo.new("bar") 100_000.times { object.val }
Runtime => 9.284 ms
```ruby
class Foo
def initialize(val)
@val = val
end
attr_reader :val
end
object = Foo.new("bar")
100_000.times { object.val }
# Runtime => 6.966 msArray rand
each VS while
Block
这里只是一些小例子,可参考 All code challenges
另外一个大神的github.
Last updated