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

D'oh. Thinko causing typoing. Make the obvious fix and the comment remains true.
  • Comment on Re: Re: Re: Structure is more important than speed

Replies are listed 'Best First'.
Re: Re: Re: Re: Structure is more important than speed
by sauoq (Abbot) on Sep 09, 2002 at 23:58 UTC
    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?