Skip to content

Commit

Permalink
Optimize ColumnNumberToName function performance, reduce about 50% me…
Browse files Browse the repository at this point in the history
…mory usage and 50% time cost (#1935)

Co-authored-by: zhayt <[email protected]>
  • Loading branch information
zhayt and zhayt committed Jul 5, 2024
1 parent 4e6457a commit b18b480
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,18 @@ func ColumnNumberToName(num int) (string, error) {
if num < MinColumns || num > MaxColumns {
return "", ErrColumnNumber
}
var col string
estimatedLength := 0
for n := num; n > 0; n = (n - 1) / 26 {
estimatedLength++
}

result := make([]byte, estimatedLength)
for num > 0 {
col = string(rune((num-1)%26+65)) + col
estimatedLength--
result[estimatedLength] = byte((num-1)%26 + 'A')
num = (num - 1) / 26
}
return col, nil
return string(result), nil
}

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

0 comments on commit b18b480

Please sign in to comment.