I am not sure if this belongs here or Programmers.SE.
I have a simple for loop with a few if statements inside. The for
loop needs to go over 200,000 elements and it is very slow in doing this. It takes over 10 minutes to run the loop and I have to run this loop 900 times. The code is below. I've timed the inside body of the loop and it is rather fast. The slow loop is because of the number of elements and NOT the body of the loop. This suggests that I can't parallelize since the "work" itself is not CPU consuming, is that correct?
Anyone have ideas on how I can improve the execution time?
//main interaction function for the day
// assumption: if a human is bit, and becomes infected.. can not get BIT again
public void Interaction()
{
// need to go through all mosquitos
for (int i = 0; i < Cnts.MosqPopulation; i++)
{
var mosq = Mosquitos[i];
// see if this mosquito will bite someone
if (mosq.bitedistribution[(int)mosq.age - 1] == 1)
{
// who will it bite? get a list of good humans to bite
// they have to non-isolated and they shouldnt have been bit before
var nonisohumans = Humans.FindAll(x => (x.health != HumanStatus.SymptomaticIsolated
&& x.swap == HumanStatus.Null));
// pick a random human from the pool of candidates to bite.
var rhuman = nonisohumans[Utils.random.Next(nonisohumans.Count)];
// if the human is susceptible and mosquito is symptomatic
if (rhuman.health == HumanStatus.Susceptible && mosq.health == MosquitoStatus.Infectious)
{
// see if the disease will transfer to him
if (Utils.random.NextDouble() < Cnts.Contgion_MtoH)
{
rhuman.swap = HumanStatus.Latent;
}
}
// if the human is (a)symptomatic and mosqutio is susceptible
if ((rhuman.health == HumanStatus.Symptomatic || rhuman.health == HumanStatus.ASymptomatic) && mosq.health == MosquitoStatus.Susceptible)
{
// see if the disease will transfer to the 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} will bite today", i);
// Console.WriteLine("Its distribution was: {0}", string.Join(",", Mosquitos[i].bitedistribution));
}
}
}