If statements inside or outside loops?

前端 未结 6 885
醉梦人生
醉梦人生 2021-02-07 17:32

Is it better if I do this:

foreach my $item ( @array ) {
   if ( $bool ) {
     .. code ..
   }
   else {
     .. code ..
   }
}

or

<         


        
6条回答
  •  再見小時候
    2021-02-07 18:35

    Simply evaluating a boolean variable as you have done here, these are roughly equivalent. However, if the variable were replaced with a complicated expression that took a long time to evaluate, the second example would be better because it would only be evaluated once.

提交回复
热议问题