[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

is defined? fast?

George Moschovitis

1/17/2005 1:22:00 PM

Hello everyone, I have a simple question:

is 'if defined?(MyFlag)' as fast as 'if $my_flag'
or is it much slower?

Any information will be appreciated.

George.

16 Answers

Robert Klemme

1/17/2005 4:07:00 PM

0


"George Moschovitis" <george.moschovitis@gmail.com> schrieb im Newsbeitrag
news:1105968115.995603.227690@f14g2000cwb.googlegroups.com...
> Hello everyone, I have a simple question:
>
> is 'if defined?(MyFlag)' as fast as 'if $my_flag'
> or is it much slower?
>
> Any information will be appreciated.

You cannot compare them as both have different functionality: defined?
checks whether a var is defined while the other test uses the var value.

I think there is a wiki page about this but I cannot find it at the
moment. The wiki is here:
http://www.rubygarde...

Regards

robert


George Moschovitis

1/17/2005 6:47:00 PM

0

of course they are different. But is defined? *much* slower than the
simple check? I 'll hava a look at the wiki

gabriele renzi

1/17/2005 8:23:00 PM

0

Robert Klemme ha scritto:
> "George Moschovitis" <george.moschovitis@gmail.com> schrieb im Newsbeitrag
> news:1105968115.995603.227690@f14g2000cwb.googlegroups.com...
>
>>Hello everyone, I have a simple question:
>>
>>is 'if defined?(MyFlag)' as fast as 'if $my_flag'
>>or is it much slower?
>>
>>Any information will be appreciated.
>
>
> You cannot compare them as both have different functionality: defined?
> checks whether a var is defined while the other test uses the var value.
>

I think he's wondering if writing stuff like

conditionalstuff
if defined? foo

vs
foo=bar
conditionalstuff
if foo

Josef 'Jupp' Schugt

1/18/2005 8:21:00 PM

0

George Moschovitis wrote:

> But is defined? *much* slower than the simple check?

Perhaps the following analogy helps understanding the problem with your
question:

When asking if a humvee is much slower than an Alfa Romeo it is a very
good idea to provide a description of the terrain one refers to: On a
motodrome an Alfa Romeo of course is much faster than a humvee. During a
traffic jam both cars are equally fast. When leaving roads the humvee
has good chances of being much faster than an Alfa Romeo (that may not
even move one meter). HTH.

Fiat tux,

Josef 'Jupp' Schugt
--
The most severe crime in Germany is being on board of a plane that is
hijacked. Separation of powers is bypassed, the only punishment for it
is death, no legal proceedigs are necessary. It is the *only* crime for
which germans may sentence you to death. Ask German DoD for details.

Mark Hubbart

1/18/2005 11:25:00 PM

0

On Mon, 17 Jan 2005 22:26:17 +0900, George Moschovitis
<george.moschovitis@gmail.com> wrote:
> Hello everyone, I have a simple question:
>
> is 'if defined?(MyFlag)' as fast as 'if $my_flag'
> or is it much slower?

A quick check suggests that defined? *might* be just slightly slower.
But probably not enough to matter. You may want to profile your
scripts (just "require 'profile'" at the top) and see what's best in
your case.

These results might be affected by caching of data; if you use
different variables for each of the 5000 checks, it could easily come
out different.

mark@eMac% ruby -rprofile
def a
5000.times{true if defined? Foo}
end
def b
5000.times{true if $foo}
end

a
b

% cumulative self self total
time seconds seconds calls ms/call ms/call name
95.92 0.47 0.47 2 235.00 235.00 Integer#times
2.04 0.48 0.01 1 10.00 10.00 Profiler__.start_profile
0.00 0.48 0.00 1 0.00 230.00 Object#b
0.00 0.48 0.00 1 0.00 490.00 #toplevel
0.00 0.48 0.00 2 0.00 0.00 Module#method_added
0.00 0.48 0.00 1 0.00 240.00 Object#a

cheers,
Mark


Eric Hodel

1/19/2005 12:30:00 AM

0

On 18 Jan 2005, at 15:24, Mark Hubbart wrote:

> On Mon, 17 Jan 2005 22:26:17 +0900, George Moschovitis
> <george.moschovitis@gmail.com> wrote:
>> Hello everyone, I have a simple question:
>>
>> is 'if defined?(MyFlag)' as fast as 'if $my_flag'
>> or is it much slower?
>
> A quick check suggests that defined? *might* be just slightly slower.
> But probably not enough to matter. You may want to profile your
> scripts (just "require 'profile'" at the top) and see what's best in
> your case.

No, profile cannot detect a global test or defined? being called,
because they are not method calls. You want to use benchmark.rb.

defined? MyFlag is at best almost half as fast as $my_flag, probably
because defined? does not just return true (it returns a String).

$ cat x.rb
require 'benchmark'

TIMES = ARGV.shift || 100_000
$x = true
@x = true
@@x = true

module X; X = true; end

Benchmark.benchmark do |bm|
bm.report "baseline " do
end

bm.report "defined? Nested::Constant" do
TIMES.times do
true if defined? X::X
true if defined? X::Y
end
end

bm.report "defined? Constant " do
TIMES.times do
true if defined? Benchmark
true if defined? NoSuchConst
end
end

bm.report "defined? $global " do
TIMES.times do
true if defined? $x
true if defined? $y
end
end

bm.report "defined? local " do
TIMES.times do
true if defined? bm
true if defined? no_such_local
end
end

bm.report "defined? @var " do
TIMES.times do
true if defined? @x
true if defined? @no_such_ivar
end
end

bm.report "defined? @@var " do
TIMES.times do
true if defined? @@x
true if defined? @@no_such_cvar
end
end

bm.report "$global " do
TIMES.times do
true if $x
true if $y
end
end
end

$ ruby x.rb
baseline 0.000000 0.000000 0.000000 ( 0.000046)
defined? Nested::Constant 0.680000 0.000000 0.680000 ( 0.884762)
defined? Constant 0.370000 0.000000 0.370000 ( 0.458034)
defined? $global 0.320000 0.000000 0.320000 ( 0.336950)
defined? local 0.330000 0.000000 0.330000 ( 0.345521)
defined? @var 0.370000 0.000000 0.370000 ( 0.455369)
defined? @@var 0.370000 0.000000 0.370000 ( 0.400793)
$global 0.150000 0.000000 0.150000 ( 0.174302)

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Robert Klemme

1/19/2005 5:41:00 PM

0


"Eric Hodel" <drbrain@segment7.net> schrieb im Newsbeitrag
news:12DE17DB-69B1-11D9-AFB0-000D93436DA0@segment7.net...

Looks like the simple var check is better performance wise. Personally I
never felt the need for defined? - only during my initial Ruby stages,
when I carried some Perl thinking with me; but never after I got rid of
that. :-)

Ah, and btw, you don't need the "true if ". Not that it matters
performance wise since you applied it everywhere - but I prefer to reduce
redundancy... :-)

Kind regards

robert

Florian Gross

1/19/2005 6:00:00 PM

0

Robert Klemme wrote:

> Personally I never felt the need for defined? - only during my
> initial Ruby stages, when I carried some Perl thinking with me; but
> never after I got rid of that. :-)

I always thought it was there mostly for using it in IRB, not in actual
code. (E.g. to ask Ruby how it understands a construct.)

But it does at least make sense in real code in the defined?(Constant) case.

Joel VanderWerf

1/19/2005 6:40:00 PM

0

Florian Gross wrote:
> Robert Klemme wrote:
>
>> Personally I never felt the need for defined? - only during my
>> initial Ruby stages, when I carried some Perl thinking with me; but
>> never after I got rid of that. :-)
>
>
> I always thought it was there mostly for using it in IRB, not in actual
> code. (E.g. to ask Ruby how it understands a construct.)
>
> But it does at least make sense in real code in the defined?(Constant)
> case.

The following idiom is useful in mixins:

module Mixin
def meth
super if defined?(super)
# add some behavior here
end
end


Eric Hodel

1/19/2005 6:45:00 PM

0

On 19 Jan 2005, at 09:46, Robert Klemme wrote:

>
> "Eric Hodel" <drbrain@segment7.net> schrieb im Newsbeitrag
> news:12DE17DB-69B1-11D9-AFB0-000D93436DA0@segment7.net...
>
> Looks like the simple var check is better performance wise.
> Personally I
> never felt the need for defined? - only during my initial Ruby stages,
> when I carried some Perl thinking with me; but never after I got rid of
> that. :-)

module M
def functionality
do_stuff if @x
end
end

class C
include M
end

C.new.functionality

With ruby -w, this will give an 'uninitialized instance variable'
warning. M#functionality needs to be written this way to get rid of
the warning:

module M
def functionality
@x = default unless defined? @x
do_stuff if @x
end
end

> Ah, and btw, you don't need the "true if ". Not that it matters
> performance wise since you applied it everywhere - but I prefer to
> reduce
> redundancy... :-)

I feel it is necessary. You're benchmarking the case of:

statement if defined? something

vs.

statement if $global

I like being specific.

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04