Precompute the power in the reverse map
diff --git a/si.go b/si.go
index f8cda7d..7b8e6ee 100644
--- a/si.go
+++ b/si.go
@@ -29,10 +29,11 @@
var revSIPrefixTable = revfmap(siPrefixTable)
+// revfmap reverses the map and precomputes the power multiplier
func revfmap(in map[float64]string) map[string]float64 {
rv := map[string]float64{}
for k, v := range in {
- rv[v] = k
+ rv[v] = math.Pow(10, k)
}
return rv
}
@@ -98,5 +99,5 @@
// func ParseFloat(s string, bitSize int) (f float64, err error)
base, err := strconv.ParseFloat(found[1], 64)
- return base * math.Pow(10, mag), unit, err
+ return base * mag, unit, err
}
diff --git a/si_test.go b/si_test.go
index 8aab59c..32fb386 100644
--- a/si_test.go
+++ b/si_test.go
@@ -90,3 +90,9 @@
t.Errorf("Expected error on x1.21JW, got %v %v", gotf, gotu)
}
}
+
+func BenchmarkParseSI(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseSI("2.2346ZB")
+ }
+}