Skip to content

insertNewRowBefore is more and more slow for large files #2509

Closed
@momala454

Description

@momala454

This is:

- [x ] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Adding new rows is always the same speed

What is the current behavior?

insertNewRowBefore to add new rows is more and more slow depending on the number of existing rows.
With 0 rows, it takes 0 seconds. With 1000 it takes 0.082s, with 2000 it takes 0.166s

What are the steps to reproduce?

with this minimal code the time is different but the same behaviour occurs.

<?php

require __DIR__ . '/vendor/autoload.php';

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

$i = 3000;
while ($i-- > 0) {
    $time = microtime(true);
    $newRowNumber = $worksheet->getHighestRow() +1;
    if ($newRowNumber === 2 && !$worksheet->cellExists('A1')) {
        // file is empty
        $newRowNumber = 1;
    }
    $time2 = microtime(true);
    $worksheet->insertNewRowBefore($newRowNumber);
    $time3 = microtime(true);
    $worksheet->fromArray(['a', 'b', 'c', 'd'], null, 'A'.$newRowNumber);

    if ($i % 500 === 0) {
        echo 'line '.$newRowNumber.' '.round($time3 - $time2, 3)."\n";
    }
}

output
line 500 0.007
line 1000 0.013
line 1500 0.021
line 2000 0.027
line 2500 0.034
line 3000 0.042

Which versions of PhpSpreadsheet and PHP are affected?

1.21.0
php 8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions