c# - how to make a loop faster without parallelizing -


i not sure if belongs here or programmers.se.

i have simple loop few if statements inside. for loop needs go on 200,000 elements , slow in doing this. takes on 10 minutes run loop , have run loop 900 times. code below. i've timed inside body of loop , rather fast. slow loop because of number of elements , not body of loop. suggests can't parallelize since "work" not cpu consuming, correct?

anyone have ideas on how can improve execution time?

 //main interaction function day     // assumption: if human bit, , becomes infected.. can not bit again     public void interaction()     {         // need go through mosquitos         (int = 0; < cnts.mosqpopulation; i++)         {             var mosq = mosquitos[i];              // see if mosquito bite                         if (mosq.bitedistribution[(int)mosq.age - 1] == 1)             {                  // bite? list of humans bite                 // have non-isolated , shouldnt have been bit before                 var nonisohumans = humans.findall(x => (x.health != humanstatus.symptomaticisolated                                                         && x.swap == humanstatus.null));                  // pick random human pool of candidates bite.                 var rhuman = nonisohumans[utils.random.next(nonisohumans.count)];                   // if human susceptible , mosquito symptomatic                 if (rhuman.health == humanstatus.susceptible && mosq.health == mosquitostatus.infectious)                 {                     // see if disease transfer him                     if (utils.random.nextdouble() < cnts.contgion_mtoh)                     {                         rhuman.swap = humanstatus.latent;                     }                 }                  // if human (a)symptomatic , mosqutio susceptible                 if ((rhuman.health == humanstatus.symptomatic || rhuman.health == humanstatus.asymptomatic) && mosq.health == mosquitostatus.susceptible)                 {                     // see if disease transfer mosquito                     double probabilityofswap;                     if (rhuman.health == humanstatus.symptomatic)                     {                         probabilityofswap = cnts.contgion_htom;                     }                     else                     {                         //reduced transmission                         probabilityofswap = cnts.contgion_htom * cnts.reductionfactor;                     }                      if (utils.random.nextdouble() < probabilityofswap)                     {                         mosq.swap = mosquitostatus.latent;                     }                 }                 // console.writeline("mosquito i:{0} bite today", i);                 // console.writeline("its distribution was: {0}", string.join(",", mosquitos[i].bitedistribution));             }          }      } 

building off oli's comment, try changing:

var nonisohumans = humans.findall(x => (x.health != humanstatus.symptomaticisolated && x.swap == humanstatus.null)); 

to:

var nonisohumans = humans.where(x => (x.health != humanstatus.symptomaticisolated && x.swap == humanstatus.null)); 

and remove loop entirely processes once. run conditional (x.health != humanstatus.symptomaticisolated && x.swap == humanstatus.null) check after random value again make sure aren't selecting 1 you've iterated through (if so, new random).

looks should change last if statement else if doesn't evaluated if previous 1 evaluates true (they mutually exclusive):

else if ((rhuman.health == humanstatus.symptomatic || rhuman.health == humanstatus.asymptomatic) && mosq.health == mosquitostatus.susceptible) 

Comments