From 34f10ff04b549aa8d38ef5fc64213854c024ee66 Mon Sep 17 00:00:00 2001 From: Matt Allan Date: Tue, 18 Dec 2018 13:17:41 -0500 Subject: [PATCH] Remove duplicate strtoupper Removing the duplicate strtoupper call has a meaningful impact on performance since this method is called at least once per cell. `Worksheet::getCells` currently calls `strtoupper` twice. `strtoupper` is kind of expensive and this method is called at least once for every cell in the spreadsheet. By removing the unnecessary second call the runtime decreases by 18% when importing a ~100K cell spreadsheet. Closes #825 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Worksheet/Worksheet.php | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43aab4094f..5d1553d0ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Support COUNTIFS multiple arguments - [#830](https://github.com/PHPOffice/PhpSpreadsheet/pull/830) - Change `libxml_disable_entity_loader()` as shortly as possible - [#819](https://github.com/PHPOffice/PhpSpreadsheet/pull/819) - Improved memory usage and performance when loading large spreadsheets - [#822](https://github.com/PHPOffice/PhpSpreadsheet/pull/822) +- Improved performance when loading large spreadsheets - [#825](https://github.com/PHPOffice/PhpSpreadsheet/pull/825) ## [1.5.2] - 2018-11-25 diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index e9d93af251..dd2339014c 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -1191,9 +1191,12 @@ public function setCellValueExplicitByColumnAndRow($columnIndex, $row, $value, $ */ public function getCell($pCoordinate, $createIfNotExists = true) { + // Uppercase coordinate + $pCoordinateUpper = strtoupper($pCoordinate); + // Check cell collection - if ($this->cellCollection->has(strtoupper($pCoordinate))) { - return $this->cellCollection->get($pCoordinate); + if ($this->cellCollection->has($pCoordinateUpper)) { + return $this->cellCollection->get($pCoordinateUpper); } // Worksheet reference? @@ -1214,9 +1217,6 @@ public function getCell($pCoordinate, $createIfNotExists = true) } } - // Uppercase coordinate - $pCoordinate = strtoupper($pCoordinate); - if (Coordinate::coordinateIsRange($pCoordinate)) { throw new Exception('Cell coordinate can not be a range of cells.'); } elseif (strpos($pCoordinate, '$') !== false) { @@ -1224,7 +1224,7 @@ public function getCell($pCoordinate, $createIfNotExists = true) } // Create new cell object, if required - return $createIfNotExists ? $this->createNewCell($pCoordinate) : null; + return $createIfNotExists ? $this->createNewCell($pCoordinateUpper) : null; } /**