vendredi 30 novembre 2018

is it better to use multiple ifs in for loop or a for loop for each if

I have the following scenario. I have large amounts of data of the following schema

{
  a: number,
  b: number,
  c: number,
  d: number
}

Say there are ~20k records of this structure. I have to do some calculations with each of the field

Calculations with a and b are mandatory, but c and d are not. I know outside of the for loop itself if I have to perform calculations on c and d or not.

So what is the optimised way to handle this situation?

option1:

workOnC = true
workOnD = false

for (i=0; i< 20k, i++) {
  work on a
  work on b
  if(workOnC) work on c
  if(workOnD) work on d
}

or

option 2

workOnC = true
workOnD = false

for(i=0;i<20k;i++) {
  work on a
  work on b
}

if(workOnC) {
 for(i=0;i<20k;i++) {
   work on c
 }
}

if(workOnD) {
 for(i=0;i<20k;i++) {
   work on d
 }
}

Aucun commentaire:

Enregistrer un commentaire