Respond_to? vs include?

Hi everybody…

I wrote a fancy and very humble benckmark test to see which is faster
between respond_to? and include?..
The script is here:

https://gist.github.com/zeroed/5282125

But I’m not so confident with the result… if someone have 3min to
lose reading my script, can tell me if I’m completly missing the point
in such kind of test? :slight_smile:
Maybe there is some improvements that needs to be done.

Thank you so much in advance…

e.

Your benchmark looks okay. I added a comment to your Gist about a fork
I made that shows the O(n) behaviour of include? versus the
constant-time behaviour of respond_to? by just tweaking your code.

On Mon, Apr 1, 2013 at 12:07 AM, Adam P. [email protected]
wrote:

Your benchmark looks okay. I added a comment to your Gist about a fork
I made that shows the O(n) behaviour of include? versus the
constant-time behaviour of respond_to? by just tweaking your code.

Thank you so much Adam.
Very interesting… appreciated.

e.

On Mon, Apr 1, 2013 at 8:26 AM, Edoardo R. [email protected]
wrote:

On Mon, Apr 1, 2013 at 12:07 AM, Adam P. [email protected] wrote:

Your benchmark looks okay. I added a comment to your Gist about a fork
I made that shows the O(n) behaviour of include? versus the
constant-time behaviour of respond_to? by just tweaking your code.

Yeah, Array lookup is O(n) - that’s what one would expect. But the
Array
creation has overhead as well.

Thank you so much Adam.

Very interesting… appreciated.

I think the situation is more dramatic: for a single method test the
#instance_methods call needs to be included.

Kind regards

robert

On 3 Apr 2013 08:49, “Robert K.” [email protected] wrote:

Yeah, Array lookup is O(n) - that’s what one would expect. But the Array
creation has overhead as well.

Good catch. Definitely a painful way to implement respond_to? !

On Wed, Apr 3, 2013 at 9:48 AM, Robert K.
[email protected] wrote:

Modified version which compares the single method check times (which need to include Mock.instance_methods(false)). · GitHub

Kind regards

robert


remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Considerably more dramatic!
Thank you so much Robert for pointed that out…

eddie.