improved runtime for converting from int

This commit is contained in:
Branden Colen
2021-04-02 23:27:08 -05:00
parent 2f52a116fe
commit 37a85dd4b7
3 changed files with 36 additions and 35 deletions
+5 -6
View File
@@ -11,13 +11,12 @@ Quickly and efficiently convert to and from roman numerals in Go.
goos: darwin
goarch: arm64
pkg: github.com/brandenc40/romannumeral
BenchmarkIntToString-8 18160285 66.43 ns/op 24 B/op 2 allocs/op
BenchmarkIntToBytes-8 18630670 63.68 ns/op 24 B/op 2 allocs/op
BenchmarkStringToInt-8 17537406 67.26 ns/op 0 B/op 0 allocs/op
BenchmarkBytesToInt-8 18248730 64.83 ns/op 0 B/op 0 allocs/op
BenchmarkIntToString-8 56474846 20.84 ns/op 0 B/op 0 allocs/op
BenchmarkIntToBytes-8 48157634 24.36 ns/op 0 B/op 0 allocs/op
BenchmarkStringToInt-8 17584252 67.28 ns/op 0 B/op 0 allocs/op
BenchmarkBytesToInt-8 18343551 64.77 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/brandenc40/romannumeral 6.283s
ok github.com/brandenc40/romannumeral 6.111s
```
### Example
+15 -11
View File
@@ -43,13 +43,22 @@ var _numerals = []numeral{
{1, []byte("I")},
}
// lookup arrays used for converting from an int to a roman numeral extremely quickly.
// inspired from https://rosettacode.org/wiki/Roman_numerals/Encode#Go
var (
m0 = []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
m1 = []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
m2 = []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
m3 = []string{"", "M", "MM", "MMM"}
)
// IntToString converts an integer value to a roman numeral string. An error is
// returned if the integer is not between 1 and 3999.
func IntToString(input int) (string, error) {
if outOfBounds(input) {
return "", IntegerOutOfBounds
}
return string(intToRoman(input)), nil
return intToRoman(input), nil
}
// IntToBytes converts an integer value to a roman numeral byte array. An error is
@@ -58,7 +67,7 @@ func IntToBytes(input int) ([]byte, error) {
if outOfBounds(input) {
return nil, IntegerOutOfBounds
}
return intToRoman(input), nil
return []byte(intToRoman(input)), nil
}
// outOfBounds checks to ensure an input value is valid for roman numerals without the need of
@@ -67,15 +76,10 @@ func outOfBounds(input int) bool {
return input < _minRoman || input > _maxRoman
}
func intToRoman(input int) []byte {
var output []byte
for _, rom := range _numerals {
for input >= rom.val {
output = append(output, rom.sym...)
input -= rom.val
}
}
return output
func intToRoman(n int) string {
// this is efficient in Go. the seven operands are evaluated,
// then a single allocation is made of the exact size needed for the result.
return m3[n%1e4/1e3] + m2[n%1e3/1e2] + m1[n%100/10] + m0[n%10]
}
// StringToInt converts a roman numeral string to an integer. Roman numerals for numbers
+16 -18
View File
@@ -6,24 +6,22 @@ import (
)
var testCases = map[string]int{
"I": 1, "II": 2,
"III": 3, "IV": 4,
"V": 5, "VI": 6,
"VII": 7, "VIII": 8,
"IX": 9, "X": 10,
"XIV": 14, "XIX": 19,
"XX": 20, "XXXIII": 33,
"XXXIV": 34, "XXXIX": 39,
"XLIX": 49, "L": 50,
"LXXXIX": 89, "XCIX": 99,
"CXLIX": 149, "CCCXLIX": 349,
"CDLVI": 456, "D": 500,
"DCIV": 604, "DCCLXXXIX": 789,
"DCCCXLIX": 849, "CMIV": 904,
"MVII": 1007, "MLXVI": 1066,
"MDCCLXXVI": 1776, "MMDCCCVI": 2806,
"MMCMXCIX": 2999, "MMXXI": 2021,
"MMMCMLXXIX": 3979, "MMMCMXCIX": 3999,
"I": 1, "II": 2, "III": 3, "IV": 4, "V": 5, "VI": 6,
"VII": 7, "VIII": 8, "IX": 9, "X": 10, "XI": 11, "XII": 12,
"XIII": 13, "XIV": 14, "XV": 15, "XVI": 16, "XVII": 17,
"XVIII": 18, "XIX": 19, "XX": 20, "XXXI": 31, "XXXII": 32,
"XXXIII": 33, "XXXIV": 34, "XXXV": 35, "XXXVI": 36, "XXXVII": 37,
"XXXVIII": 38, "XXXIX": 39, "XL": 40, "XLI": 41, "XLII": 42,
"XLIII": 43, "XLIV": 44, "XLV": 45, "XLVI": 46, "XLVII": 47,
"XLVIII": 48, "XLIX": 49, "L": 50, "LXXXIX": 89, "XC": 90,
"XCI": 91, "XCII": 92, "XCIII": 93, "XCIV": 94, "XCV": 95,
"XCVI": 96, "XCVII": 97, "XCVIII": 98, "XCIX": 99, "C": 100,
"CI": 101, "CII": 102, "CIII": 103, "CIV": 104, "CV": 105,
"CVI": 106, "CVII": 107, "CVIII": 108, "CIX": 109, "CXLIX": 149,
"CCCXLIX": 349, "CDLVI": 456, "D": 500, "DCIV": 604, "DCCLXXXIX": 789,
"DCCCXLIX": 849, "CMIV": 904, "M": 1000, "MVII": 1007, "MLXVI": 1066,
"MCCXXXIV": 1234, "MDCCLXXVI": 1776, "MMXXI": 2021, "MMDCCCVI": 2806,
"MMCMXCIX": 2999, "MMM": 3000, "MMMCMLXXIX": 3979, "MMMCMXCIX": 3999,
}
func TestIntToString(t *testing.T) {