private void CalculateFitness(TimeTable timeTable)
{
int score = 0, DAYS_NUM = 5;
score = timeTable.Exams.SelectMany(exam => exam.Students)
.GroupBy(s => s)
.Select(g => Connections(g.Count()))
.Sum();
timeTable.Fitness = score;
}
int Connections(int corners)
{
// 0+1+2+...+(corners-1)
return corners * (corners - 1) / 2;
}
Asked
Active
Viewed 376 times
0

Peter Mortensen
- 30,738
- 21
- 105
- 131

Romaine Carter
- 637
- 11
- 23
-
5that variable assignment is the bottleneck of your application?? – BrokenGlass Jan 12 '11 at 14:49
-
1It's hard to suggest improvements without knowing the types involved, the relationships etc. I'm sure there's a better approach, but we can't really help much without more details. – Jon Skeet Jan 12 '11 at 14:52
-
2I don't think so...this is an app for physical fitness, so maybe it's supposed to help people who have "bottle" necks. – Chris Pfohl Jan 12 '11 at 14:53
-
Delayed execution of LINQ on that line? I guess the intersect is the real bottleneck - however he's calling the Count() inside the loop so it really ought not be delayed. – Rup Jan 12 '11 at 14:53
-
1The last line is cheap compared to the rest of the function, unless the setter of `timeTable.Fitness` is very expensive. But you didn't post that one. – CodesInChaos Jan 12 '11 at 15:26
-
Is this a simple object you've written or e.g. derived from a genetic programming framework where the Fitness setter might be very complicated? – Rup Jan 12 '11 at 15:30
1 Answers
1
Isn't your function equivalent to this:
score = timeTable.Exams.SelectMany(exam=>exam.Students)
.GroupBy(s=>s)
.Select(g=>Connections(g.Count()))
.Sum();
with helper function
int Connections(int corners)
{
//Formula for number of sides in a complete graph
//http://en.wikipedia.org/wiki/Complete_graph
// 0+1+2+...+(corners-1)
return corners*(corners-1)/2;
}
This should be linear runtime in timeTable.Exams.Sum(exam=>exam.Student.Count())
whereas yours looks quadratic to me.

CodesInChaos
- 106,488
- 23
- 218
- 262
-
Ok timetable is a list of exam objects with each exam containing a list of students. I would like to count the number of exams that contain students who have the same id. This would effectively give me a clash count. However my current code is moving very slow when i try to assign the score to the timetable.fitness – Romaine Carter Jan 12 '11 at 15:14
-
@user: Does your code give different results from mine? I calculate in how many exams each student is, and then use the `Connections` function to quickly calculate the collision count for each student, and then sum them up. – CodesInChaos Jan 12 '11 at 15:16
-
-
-
I edited the function to include your suggestions however i still have the same problem. timetabe.Fitness is a float auto-property – Romaine Carter Jan 12 '11 at 15:37
-
Does it give the same results? I haven't tested it, so I'm not 100% sure. But you didn't describe your problem well. – CodesInChaos Jan 12 '11 at 16:28
-
Yes it gives the same result performance wise when i ran ant code profiler on it. however when i remove the assignment to timetable.fitness it runs at optimal speed. – Romaine Carter Jan 12 '11 at 19:04