Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IXLRows delete method's runtime is quadratic #2261

Open
sechel opened this issue Jan 16, 2024 · 0 comments
Open

IXLRows delete method's runtime is quadratic #2261

sechel opened this issue Jan 16, 2024 · 0 comments

Comments

@sechel
Copy link
Contributor

sechel commented Jan 16, 2024

In contract to what is stated in the documentation the runtime of IXLRows.Delete() is quadratic, see

kp.Key.Row(r).Delete();

In my case replacing IXLRows.Delete() by IXLRange.Delete() has huge performance gains.

public void Delete()
{
    if (_worksheet != null)
    {
        _worksheet.Internals.RowsCollection.Clear();
        _worksheet.Internals.CellsCollection.Clear();
    }
    else
    {
        var toDelete = new Dictionary<IXLWorksheet, List<Int32>>();
        foreach (XLRow r in Rows)
        {
            if (!toDelete.TryGetValue(r.Worksheet, out List<Int32> list))
            {
                list = new List<Int32>();
                toDelete.Add(r.Worksheet, list);
            }

            list.Add(r.RowNumber());
        }

        foreach (KeyValuePair<IXLWorksheet, List<int>> kp in toDelete)
        {
            foreach (int r in kp.Value.OrderByDescending(r => r))
                kp.Key.Row(r).Delete();
        }
    }
}
@sechel sechel changed the title IXLRows runtime is quadratic IXLRows delete method's runtime is quadratic Jan 16, 2024
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

No branches or pull requests

1 participant