If statements inside or outside loops?

前端 未结 6 961
醉梦人生
醉梦人生 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:09

    If you're optimizing for speed, the second (foreach loops inside the if branches) should be faster, since you won't be doing the test in each loop iteration.

提交回复
热议问题