Skip to content

Commit

Permalink
Cell width calculation performance (#11)
Browse files Browse the repository at this point in the history
* Readme updated.

* Cell width calculation performance optimizations. Updated readme.
  • Loading branch information
nimmneun committed Aug 20, 2016
1 parent 6844e6e commit daac9dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -77,12 +77,13 @@ Style::setBorderDiagonalDown(style, color)
```

### Cell autosizing
#### ... is cool, but comes with serious performance impacts!
| Impacts of autosizing | 100k rows * 10 cols * 5 chars | 100k rows * 10 cols * 10 chars | 100k rows * 10 cols * 20 chars | 100k rows * 10 cols * 40 chars |
| ------------------------ | ----------------------------- | ------------------------------ | ------------------------------ | ------------------------------ |
| Autosizing DISABLED | 22 seconds | 22 seconds | 23 seconds | 24 seconds |
| Autosizing ENABLED | 30 seconds | 34 seconds | 40 seconds | 53 seconds |
| Performance loss in % | + 36 % | + 54 % | + 73 % | + 112 % |
#### ... is cool, but it comes with serious performance impacts, especially when dealing with multibyte characters (like ä, ß, Æ, ポ).
| Impacts of autosizing | 100k rows * 10 cols * 5 chars | 100k rows * 10 cols * 10 chars | 100k rows * 10 cols * 20 chars | 100k rows * 10 cols * 40 chars |
| ------------------------------------- | ----------------------------- | ------------------------------ | ------------------------------ | ------------------------------ |
| Autosizing OFF (Single Byte Chars) | 24 seconds | 24 seconds | 24 seconds | 26 seconds |
| Autosizing ON (Single Byte Chars) | 30 seconds (+ 25 %) | 32 seconds (+ 33 %) | 35 seconds (+ 45 %) | 43 seconds (+ 65 %) |
| Autosizing OFF (Multi Byte Chars) | 27 seconds | 28 seconds | 30 seconds | 31 seconds |
| Autosizing ON (Multi Byte Chars) | 36 seconds (+ 33 %) | 41 seconds (+ 46 %) | 49 seconds (+ 63 %) | 64 seconds (+ 106 %) |

### Additional examples
```php
Expand Down
17 changes: 16 additions & 1 deletion src/OneSheet/Width/WidthCalculator.php
Expand Up @@ -42,7 +42,8 @@ public function __construct(WidthCollection $widthCollection)
public function getCellWidth($value, Font $font)
{
$width = 0.07 * $font->getSize();
foreach (preg_split('~~u', $value, -1, PREG_SPLIT_NO_EMPTY) as $character) {

foreach ($this->getSingleCharacterArray($value) as $character) {
if (isset($this->characterWidths[$character])) {
$width += $this->characterWidths[$character];
} elseif (strlen($character)) {
Expand All @@ -62,4 +63,18 @@ public function setFont(Font $font)
{
$this->characterWidths = $this->widthCollection->get($font->getName(), $font->getSize());
}

/**
* Split value into individual characters.
*
* @param mixed $value
* @return array
*/
private function getSingleCharacterArray($value)
{
if (mb_strlen($value) == strlen($value)) {
return str_split($value);
}
return preg_split('~~u', $value, -1, PREG_SPLIT_NO_EMPTY);
}
}

0 comments on commit daac9dc

Please sign in to comment.