Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Kernel.=== in case statements, even though I never called it
Jesse Silliman
9/5/2006 12:18:00 AM
I was profiling a script I wrote, and the method with the greatest
amount of time used was Kernel.=== . I was surprised, not only since I
didn't use this method in my code, but my code had nothing involving
modules. When I changed a case statement that was inside my loop to an
if and many elsifs, I saved about 2 seconds on profiling, and the call
was gone. Is there a reason such a slow call is used in case statements?
--
Posted via
http://www.ruby-...
.
3 Answers
Tomasz Wegrzanowski
9/5/2006 12:23:00 AM
0
On 9/5/06, Jesse Silliman <wierdbro@gmail.com> wrote:
> I was profiling a script I wrote, and the method with the greatest
> amount of time used was Kernel.=== . I was surprised, not only since I
> didn't use this method in my code, but my code had nothing involving
> modules. When I changed a case statement that was inside my loop to an
> if and many elsifs, I saved about 2 seconds on profiling, and the call
> was gone. Is there a reason such a slow call is used in case statements?
case uses === (pattern match) not ==.
It is very useful:
case str
when /foo/
...
end
case num
when 3..5
...
end
--
Tomasz Wegrzanowski [
http://t-a-w.blo...
]
Jesse Silliman
9/5/2006 12:28:00 AM
0
Tomasz Wegrzanowski wrote:
>
> case uses === (pattern match) not ==.
>
> It is very useful:
>
> case str
> when /foo/
> ...
> end
>
> case num
> when 3..5
> ...
> end
Oh, I'm quite aware of that. Oh... its calling the objects inherited
Kernel.===, not calling Kernel::===. Ok, well, in that case, a good
question would be "Why is it so much slower in simple cases than == is,
even though for string they do the exact same thing?"
--
Posted via
http://www.ruby-...
.
Tomasz Wegrzanowski
9/5/2006 1:20:00 AM
0
On 9/5/06, Jesse Silliman <wierdbro@gmail.com> wrote:
> Oh, I'm quite aware of that. Oh... its calling the objects inherited
> Kernel.===, not calling Kernel::===. Ok, well, in that case, a good
> question would be "Why is it so much slower in simple cases than == is,
> even though for string they do the exact same thing?"
Kernel#=== calls == method, not aliases it. So you have 2 method calls,
and two dynamic dispatches instead of one: Kernel#===, then String#==.
You can test this by alias_method'ing === to == in String class ;-)
Relevant code:
VALUE
rb_equal(VALUE obj1, VALUE obj2)
{
VALUE result;
if (obj1 == obj2) return Qtrue;
result = rb_funcall(obj1, id_eq, 1, obj2);
if (RTEST(result)) return Qtrue;
return Qfalse;
}
--
Tomasz Wegrzanowski [
http://t-a-w.blo...
]
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Kernel.=== in case statements, even though I never called it
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password