in reply to Badly need to call a function...:-)

I have to know. Why would someone use "if 1 == 1"? Is that part of the obfuscation or is it actually in the code?


($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print
  • Comment on Re: Badly need to call a function...:-)

Replies are listed 'Best First'.
Re^2: Badly need to call a function...:-)
by eibwen (Friar) on Apr 17, 2005 at 00:13 UTC
    As far as I can tell, if (1==1) { } is being used as an alternative to commenting out the lines. While this may provide a simple way to skip a block of code by changing a single character, there appears to be a performance detriment:

    #!/usr/bin/perl -w use strict; if (1==1) { 1; }
    $ perl -MO=Deparse equal BEGIN { $^W = 1; } use strict 'refs'; do { '???': };
    #!/usr/bin/perl -w use strict; if (0==1) { 1; }
    $ perl -MO=Deparse unequal BEGIN { $^W = 1; } use strict 'refs'; do { '???' };
    #!/usr/bin/perl -w use strict; if (1==0) { 1; }
    $ perl -MO=Deparse unequal2 BEGIN { $^W = 1; } use strict 'refs'; do { '???' };

    #!/usr/bin/perl -w use strict; 1;
    $ perl -MO=Deparse uncommented BEGIN { $^W = 1; } use strict 'refs'; '???':
    #!/usr/bin/perl -w use strict; # 1;
    $ perl -MO=Deparse commented BEGIN { $^W = 1; }

    UPDATE: Added the if (1==1) { } corollary

Re^2: Badly need to call a function...:-)
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 05:25 UTC
    Dear pedant and all gurus: I am very pleased with your attention, didn't expect anything like that.
    regarding (1==1): gentlement, c'mon, I only wanted to "kind-of-preserve" the "if" statement without exposing the business logic.
    Greately surprised that this sort of things can have a performance implications. From the other hand, curious: can these tiny details have an impact on real-world stuff?
      The performance implication depends on your implementation. If it's a critical application, particularly one that iterates significantly, the difference may be significant. Conversely, if the code isn't iterative, or only sees a few iterations, the difference is likely neglible. Given this understanding, you may want to benchmark the codes using the Benchmark module:

      use Benchmark qw(cmpthese); @data = (1,2,3,4); # the values that would have been in @_ my $minCPUsec = 15; cmpthese(-$minCPUsec, { with => \&sub_with_ifs, without => \&sub_without_ifs });

      And change @_ in the referenced subs to @data.

      Additionally, you may be interested in the Benchmarking Your Code tutorial, but I'd recommend using cmpthese instead of timethese.