Skip to main content

Practice ยท 2 of 2

LINQ vs loop: measured verdict

CHALLENGE
Difficulty: advanced+25 XP

Implement static int CountEvenLinq(int[] values) (values.Count(x => x % 2 == 0)) and static int CountEvenLoop(int[] values) (plain for-loop). Both must return the same count. Then implement static long AllocDelta(Func<int[]> work, int iterations) โ€” warmup + per-thread allocation delta โ€” and use it in the test to compare BOTH counters' allocations. The graded insight: for this workload the loop allocates 0; document the LINQ cost in the implementation comment.

Back to lesson: Practice: Performance drills