Skip to content

Commit b18b480

Browse files
zhaytzhayt
andauthored
Optimize ColumnNumberToName function performance, reduce about 50% memory usage and 50% time cost (qax-os#1935)
Co-authored-by: zhayt <[email protected]>
1 parent 4e6457a commit b18b480

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,18 @@ func ColumnNumberToName(num int) (string, error) {
232232
if num < MinColumns || num > MaxColumns {
233233
return "", ErrColumnNumber
234234
}
235-
var col string
235+
estimatedLength := 0
236+
for n := num; n > 0; n = (n - 1) / 26 {
237+
estimatedLength++
238+
}
239+
240+
result := make([]byte, estimatedLength)
236241
for num > 0 {
237-
col = string(rune((num-1)%26+65)) + col
242+
estimatedLength--
243+
result[estimatedLength] = byte((num-1)%26 + 'A')
238244
num = (num - 1) / 26
239245
}
240-
return col, nil
246+
return string(result), nil
241247
}
242248

243249
// CellNameToCoordinates converts alphanumeric cell name to [X, Y] coordinates

0 commit comments

Comments
 (0)