Optimize ColumnNumberToName function performance, reduce about 50% memory usage and 50% time cost (#1935)

Co-authored-by: zhayt <zaibek@wtotem.com>
This commit is contained in:
Aybek 2024-07-05 11:34:02 +05:00 committed by GitHub
parent 4e6457accd
commit b18b48099b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 3 deletions

12
lib.go
View File

@ -232,12 +232,18 @@ func ColumnNumberToName(num int) (string, error) {
if num < MinColumns || num > MaxColumns { if num < MinColumns || num > MaxColumns {
return "", ErrColumnNumber return "", ErrColumnNumber
} }
var col string estimatedLength := 0
for n := num; n > 0; n = (n - 1) / 26 {
estimatedLength++
}
result := make([]byte, estimatedLength)
for num > 0 { for num > 0 {
col = string(rune((num-1)%26+65)) + col estimatedLength--
result[estimatedLength] = byte((num-1)%26 + 'A')
num = (num - 1) / 26 num = (num - 1) / 26
} }
return col, nil return string(result), nil
} }
// CellNameToCoordinates converts alphanumeric cell name to [X, Y] coordinates // CellNameToCoordinates converts alphanumeric cell name to [X, Y] coordinates