in reply to Re^6: Undefined vs empty string
in thread Undefined vs empty string

I was actually kind of hoping that the example showed just that.

Here's an even mix of possible values including the empty string which you omitted, and the results are too close to call.:

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our @tests = ( (undef) x1000, ('') x1000, (chr(0)) x1000, (0) x1000, (1) x1000, ('fred') x1000, ); cmpthese -1, { a => q[ !(defined && length() ) and 1 for @tests; ], b => q[ !defined || $_ eq '' and 1 for @tests; ], }; __END__ C:\test>junk Rate b a b 1029/s -- -17% a 1239/s 20% -- C:\test>junk Rate b a b 1065/s -- -1% a 1079/s 1% --

Much of a muchness.

I guess you could get into varying the proportions of the mix commensurate with your application expectations, but ...


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^8: Undefined vs empty string
by Tux (Canon) on Jun 05, 2013 at 12:32 UTC

    How does my defined-or solution compare on your system?

    Where did I omit the empty string in

    foreach $a (undef, "", 0, 1, "x" x 60) {

    ?

    update new bench from your example. (my dor outperforms the other two consistently)

    $ cat test.pl use strict; use Benchmark qw[ cmpthese ]; our @tests = ( (undef) x 1000, ("") x 1000, (chr(0)) x 1000, (0) x 1000, (1) x 1000, ("fred") x 1000, ); cmpthese -1, { a => q[ !(defined && length() ) and 1 for @tests; ], b => q[ !defined || $_ eq "" and 1 for @tests; ], c => q[ ($_ // "") eq "" and 1 for @tests; ], }; $ repeat 5 perl test.pl Rate b a c b 1950/s -- -18% -33% a 2379/s 22% -- -18% c 2901/s 49% 22% -- Rate b a c b 1950/s -- -22% -33% a 2511/s 29% -- -13% c 2901/s 49% 16% -- Rate b a c b 1950/s -- -23% -32% a 2536/s 30% -- -12% c 2875/s 47% 13% -- Rate b a c b 1914/s -- -22% -32% a 2466/s 29% -- -12% c 2801/s 46% 14% -- Rate b a c b 1950/s -- -23% -32% a 2536/s 30% -- -12% c 2875/s 47% 13% --

    Enjoy, Have FUN! H.Merijn
      Where did I omit the empty string in

      Damn. My eyes are getting like an old CRT. They are taking longer and longer to warm up in the mornings :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.