in reply to Re: Structure is more important than speed
in thread Which way is faster?

If you throw away warnings you can double speed with
if (defined(my $foo = exists $lookup{$bar})) { .... }
instead of
if (exists $lookup{$bar}) { my $foo = $lookup{$bar}; ... }

Maybe you can, but it hardly matters as they do two entirely different things. The first sets $foo to either 1 or "" (either of which is always defined.)

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Structure is more important than speed
by Anonymous Monk on Sep 09, 2002 at 23:46 UTC
    D'oh. Thinko causing typoing. Make the obvious fix and the comment remains true.
      D'oh. Thinko causing typoing. Make the obvious fix and the comment remains true.

      I don't see an "obvious" fix that will make

      if (defined(my $foo = exists $hash{$key})) { # ... }
      work like
      if (exists $hash{$key}) { my $foo = $hash{$key}; # ... }

      I guess that's either because there isn't one or because I'm not enlightened. Would you enlighten me?

      Before you answer: Please notice that exists $hash{$key}; is not equivalent to defined $hash{$key};.

      -sauoq
      "My two cents aren't worth a dime.";
      
        If undef is not a valid value in your data structure, then:
        if (defined(my $foo = $hash{$key})) { # ... }
        You are right. That is an assumption. Very often you can tell that it is true. Many optimizations add assumptions. Don't do them if the assumptions are wrong. Don't do them if you don't need the speed. Programmers should know this.

        That assumption was obviously right and I needed the speed. Adding that assumption seemed better than rewriting in C. Programmers sometimes must make such choices. That choice worked.

        Do you feel enlightened about my reasoning?