General: Work item 16359 - [Patch] faster stringFromColumnIndex()

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@83538 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2011-12-02 13:40:40 +00:00
parent bbc73039e6
commit 207f6f5818
2 changed files with 33 additions and 9 deletions

View File

@ -669,6 +669,14 @@ class PHPExcel_Cell
*/ */
public static function columnIndexFromString($pString = 'A') public static function columnIndexFromString($pString = 'A')
{ {
// Using a lookup cache adds a slight memory overhead, but boosts speed
// caching using a static within the method is faster than a class static,
// though it's additional memory overhead
static $_indexCache = array();
if (isset($_indexCache[$pString]))
return $_indexCache[$pString];
// It's surprising how costly the strtoupper() and ord() calls actually are, so we use a lookup array rather than use ord() // It's surprising how costly the strtoupper() and ord() calls actually are, so we use a lookup array rather than use ord()
// and make it case insensitive to get rid of the strtoupper() as well. Because it's a static, there's no significant // and make it case insensitive to get rid of the strtoupper() as well. Because it's a static, there's no significant
// memory overhead either // memory overhead either
@ -683,11 +691,14 @@ class PHPExcel_Cell
// for improved performance // for improved performance
if (isset($pString{0})) { if (isset($pString{0})) {
if (!isset($pString{1})) { if (!isset($pString{1})) {
return $_columnLookup[$pString]; $_indexCache[$pString] = $_columnLookup[$pString];
return $_indexCache[$pString];
} elseif(!isset($pString{2})) { } elseif(!isset($pString{2})) {
return $_columnLookup[$pString{0}] * 26 + $_columnLookup[$pString{1}]; $_indexCache[$pString] = $_columnLookup[$pString{0}] * 26 + $_columnLookup[$pString{1}];
return $_indexCache[$pString];
} elseif(!isset($pString{3})) { } elseif(!isset($pString{3})) {
return $_columnLookup[$pString{0}] * 676 + $_columnLookup[$pString{1}] * 26 + $_columnLookup[$pString{2}]; $_indexCache[$pString] = $_columnLookup[$pString{0}] * 676 + $_columnLookup[$pString{1}] * 26 + $_columnLookup[$pString{2}];
return $_indexCache[$pString];
} }
} }
throw new Exception("Column string index can not be " . ((isset($pString{0})) ? "longer than 3 characters" : "empty") . "."); throw new Exception("Column string index can not be " . ((isset($pString{0})) ? "longer than 3 characters" : "empty") . ".");
@ -701,13 +712,25 @@ class PHPExcel_Cell
*/ */
public static function stringFromColumnIndex($pColumnIndex = 0) public static function stringFromColumnIndex($pColumnIndex = 0)
{ {
// Using a lookup cache adds a slight memory overhead, but boosts speed
// caching using a static within the method is faster than a class static,
// though it's additional memory overhead
static $_indexCache = array();
if (!isset($_indexCache[$pColumnIndex])) {
// Determine column string // Determine column string
if ($pColumnIndex < 26) { if ($pColumnIndex < 26) {
return chr(65 + $pColumnIndex); $_indexCache[$pColumnIndex] = chr(65 + $pColumnIndex);
} elseif ($pColumnIndex < 702) { } elseif ($pColumnIndex < 702) {
return chr(64 + ($pColumnIndex / 26)).chr(65 + $pColumnIndex % 26); $_indexCache[$pColumnIndex] = chr(64 + ($pColumnIndex / 26)) .
chr(65 + $pColumnIndex % 26);
} else {
$_indexCache[$pColumnIndex] = chr(64 + (($pColumnIndex - 26) / 676)) .
chr(65 + ((($pColumnIndex - 26) % 676) / 26)) .
chr(65 + $pColumnIndex % 26);
} }
return chr(64 + (($pColumnIndex - 26) / 676)).chr(65 + ((($pColumnIndex - 26) % 676) / 26)).chr(65 + $pColumnIndex % 26); }
return $_indexCache[$pColumnIndex];
} }
/** /**

View File

@ -48,6 +48,7 @@ Fixed in SVN:
- General: (MBaker) Work item 16643 - Add file directory as a cache option for cache_to_discISAM - General: (MBaker) Work item 16643 - Add file directory as a cache option for cache_to_discISAM
- General: (MBaker) Work item 16923 - Datatype.php & constant TYPE_NULL - General: (MBaker) Work item 16923 - Datatype.php & constant TYPE_NULL
- General: (MBaker) Ensure use of system temp directory for all temporary work files, unless explicitly specified - General: (MBaker) Ensure use of system temp directory for all temporary work files, unless explicitly specified
- General: (char101) Work item 16359 - [Patch] faster stringFromColumnIndex()
2011-02-27 (v1.7.6): 2011-02-27 (v1.7.6):