vendredi 14 décembre 2018

Difference between if inside or outside loop in R

What's the difference between run a if conditions inside or outside a loop?

    i=sample(10,1:100)


mbm=microbenchmark(

model1={z1=0
      for (j in 1:1000){ 
          if((j %in% i)==FALSE){
            z1=z1+1
          }
      }
      },
model2={z2=0
      for (j in 1:1000) if((j %in% i)==FALSE){
          z2=z2+1
          }
      },
times=500)
autoplot(mbm)

is it second formula only start loop if conditions is false?

Aucun commentaire:

Enregistrer un commentaire