Skip to content

O(n²) loop in OpenXmlElementEqualityComparer.cs#2041

Open
SimonCropp wants to merge 1 commit intodotnet:mainfrom
SimonCropp:O(n²)-loop-in-OpenXmlElementEqualityComparer.cs

Hidden character warning

The head ref may contain hidden characters: "O(n\u00b2)-loop-in-OpenXmlElementEqualityComparer.cs"
Open

O(n²) loop in OpenXmlElementEqualityComparer.cs#2041
SimonCropp wants to merge 1 commit intodotnet:mainfrom
SimonCropp:O(n²)-loop-in-OpenXmlElementEqualityComparer.cs

Conversation

@SimonCropp
Copy link
Copy Markdown

  for (int i = 0; i < x.ExtendedAttributes.Count(); i++)  // Count() re-enumerates each iteration
  {
      if (!x.ExtendedAttributes.ElementAt(i).Equals(y.ExtendedAttributes.ElementAt(i)))  // ElementAt is O(n)

Count() is called every loop iteration (re-enumerates), and ElementAt(i) is O(n) on IEnumerable, making the entire loop O(n³). Fix: materialize to a list/array once, or use SequenceEqual / paired foreach with two enumerators.

  for (int i = 0; i < x.ExtendedAttributes.Count(); i++)  // Count() re-enumerates each iteration
  {
      if (!x.ExtendedAttributes.ElementAt(i).Equals(y.ExtendedAttributes.ElementAt(i)))  // ElementAt is O(n)

  Count() is called every loop iteration (re-enumerates), and ElementAt(i) is O(n) on IEnumerable, making the entire  loop O(n³). Fix: materialize to a list/array once, or use SequenceEqual / paired foreach with two enumerators.
@github-actions
Copy link
Copy Markdown

Test Results

    68 files  + 1      68 suites  +1   1h 41m 9s ⏱️ + 6m 30s
 2 068 tests ± 0   2 065 ✅ ± 0   3 💤 ±0  0 ❌ ±0 
37 887 runs  +22  37 845 ✅ +22  42 💤 ±0  0 ❌ ±0 

Results for commit 37292f5. ± Comparison against base commit 40ad85b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants