0

I am building a statistics (multi-agents) application, I'm using EF5 to retrive a list of agents from agents table. it's a very large amount of data (arround 50 000 agents per region) needed to be loaded at a time to proceed some immigration logic on them. I found that the (Region).Agents navigation property is very very slow to load, but, executing the same query takes just 1 sec on Sql server. and the foreach too takes a considerable time looping.

foreach (OrigineAgent origineAgent in new List<Agent> Origine.Agents.AsParallel()))
{
    origineAgent.ScoreCalculator = _container.Resolve<IScoreCalculator>();
    origineAgent.AgentExoVariables = AgentVars;
    _migrationManager.Migrate(origineAgent);
}

What do I have to do to increase data loading and looping performance?

1
  • 1
    Move score calculator resolving out from the loop Commented Apr 13, 2013 at 12:01

2 Answers 2

2

How about that:

var resolved = _container.Resolve<IScoreCalculator>();
foreach (OrigineAgent origineAgent in Origine.Agents.ToList().AsParallel())
{
    origineAgent.ScoreCalculator = resolved
    origineAgent.AgentExoVariables = AgentVars;
    _migrationManager.Migrate(origineAgent);
}

I've moved _container.Resolve<IScoreCalculator>(); before the loop and fixed your foreach loop source, because the one from your question does not even compile.

Sign up to request clarification or add additional context in comments.

2 Comments

You're right, but your answer still not resolving performance issue of agents list from database
How about starting a separate task for _migrationManager.Migrate(originAgent) and store the task instance in an array. Then at the end of the foreach loop call WaitAll() on that array of tasks?
1

Building on MarcinJuraszek's post above, how about following:

List<Task> tasks = new List<Task>();
var resolved = _container.Resolve<IScoreCalculator>();
foreach (OrigineAgent origineAgent in Origine.Agents.ToList().AsParallel())
{
    origineAgent.ScoreCalculator = resolved
    origineAgent.AgentExoVariables = AgentVars;
    Task t = Task.Factory.StartNew((arg) => { _migrationManager.Migrate((OrigineAgent)arg); }, origineAgent);
    tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());

It parallelises calls to _migrationManager.Migrate(origineAgent);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.