diff options
author | Dean Karn <Dean.Karn@metricaid.com> | 2016-09-11 23:22:25 +0300 |
---|---|---|
committer | Dean Karn <Dean.Karn@metricaid.com> | 2016-09-11 23:22:25 +0300 |
commit | a75cdd71617f0998215f56f506ec9aabead36101 (patch) | |
tree | 5e2057a519c8cd8060a923888895c6285f74532b /en | |
parent | c83f384137b60783b605590315e9791a34829443 (diff) |
a few more low level optimizationsbytes-to-string
Diffstat (limited to 'en')
-rw-r--r-- | en/benchmarks_test.go | 252 | ||||
-rw-r--r-- | en/en.go | 4 |
2 files changed, 254 insertions, 2 deletions
diff --git a/en/benchmarks_test.go b/en/benchmarks_test.go new file mode 100644 index 00000000..c2ae6b0b --- /dev/null +++ b/en/benchmarks_test.go @@ -0,0 +1,252 @@ +package en + +import ( + "testing" + "time" + + "github.com/go-playground/locales/currency" +) + +func BenchmarkFmtNumber(b *testing.B) { + + trans := New() + f64 := float64(1234567.43) + precision := uint64(2) + + b.ResetTimer() + + b.Run("", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtNumber(f64, precision) + } + }) + + b.Run("Parallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtNumber(f64, precision) + } + }) + }) +} + +func BenchmarkFmtPercent(b *testing.B) { + + trans := New() + f64 := float64(97.05) + precision := uint64(2) + + b.ResetTimer() + + b.Run("", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtPercent(f64, precision) + } + }) + + b.Run("Parallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtPercent(f64, precision) + } + }) + }) +} + +func BenchmarkFmtCurrency(b *testing.B) { + + trans := New() + f64 := float64(1234567.43) + precision := uint64(2) + + b.ResetTimer() + + b.Run("", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtCurrency(f64, precision, currency.USD) + } + }) + + b.Run("Parallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtCurrency(f64, precision, currency.USD) + } + }) + }) +} + +func BenchmarkFmtAccounting(b *testing.B) { + + trans := New() + f64 := float64(1234567.43) + precision := uint64(2) + + b.ResetTimer() + + b.Run("", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtAccounting(f64, precision, currency.USD) + } + }) + + b.Run("Parallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtAccounting(f64, precision, currency.USD) + } + }) + }) +} + +func BenchmarkFmtDate(b *testing.B) { + + trans := New() + t := time.Now() + + b.ResetTimer() + + b.Run("FmtDateShort", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtDateShort(t) + } + }) + + b.Run("FmtDateShortParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtDateShort(t) + } + }) + }) + + b.Run("FmtDateMedium", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtDateMedium(t) + } + }) + + b.Run("FmtDateMediumParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtDateMedium(t) + } + }) + }) + + b.Run("FmtDateLong", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtDateLong(t) + } + }) + + b.Run("FmtDateLongParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtDateLong(t) + } + }) + }) + + b.Run("FmtDateFull", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtDateFull(t) + } + }) + + b.Run("FmtDateFullParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtDateFull(t) + } + }) + }) +} + +func BenchmarkFmtTime(b *testing.B) { + + trans := New() + t := time.Now() + + b.ResetTimer() + + b.Run("FmtTimeShort", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtTimeShort(t) + } + }) + + b.Run("FmtTimeShortParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtTimeShort(t) + } + }) + }) + + b.Run("FmtTimeMedium", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtTimeMedium(t) + } + }) + + b.Run("FmtTimeMediumParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtTimeMedium(t) + } + }) + }) + + b.Run("FmtTimeLong", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtTimeLong(t) + } + }) + + b.Run("FmtTimeLongParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtTimeLong(t) + } + }) + }) + + b.Run("FmtTimeFull", func(b *testing.B) { + for i := 0; i < b.N; i++ { + trans.FmtTimeFull(t) + } + }) + + b.Run("FmtTimeFullParallel", func(b *testing.B) { + + b.RunParallel(func(pb *testing.PB) { + + for pb.Next() { + trans.FmtTimeFull(t) + } + }) + }) +} @@ -71,7 +71,7 @@ func New() locales.Translator { erasAbbreviated: []string{"BC", "AD"}, erasNarrow: []string{"B", "A"}, erasWide: []string{"Before Christ", "Anno Domini"}, - timezones: map[string]string{"CLST": "Chile Summer Time", "BOT": "Bolivia Time", "EDT": "Eastern Daylight Time", "AEST": "Australian Eastern Standard Time", "CLT": "Chile Standard Time", "WITA": "Central Indonesia Time", "ACWST": "Australian Central Western Standard Time", "CHAST": "Chatham Standard Time", "MST": "Macau Standard Time", "GMT": "Greenwich Mean Time", "OESZ": "Eastern European Summer Time", "LHDT": "Lord Howe Daylight Time", "MEZ": "Central European Standard Time", "BT": "Bhutan Time", "WESZ": "Western European Summer Time", "ADT": "Atlantic Daylight Time", "TMT": "Turkmenistan Standard Time", "SAST": "South Africa Standard Time", "ART": "Argentina Standard Time", "HKT": "Hong Kong Standard Time", "CDT": "Central Daylight Time", "MYT": "Malaysia Time", "SRT": "Suriname Time", "CAT": "Central Africa Time", "MESZ": "Central European Summer Time", "UYT": "Uruguay Standard Time", "EAT": "East Africa Time", "ECT": "Ecuador Time", "ACDT": "Australian Central Daylight Time", "WIB": "Western Indonesia Time", "HAST": "Hawaii-Aleutian Standard Time", "UYST": "Uruguay Summer Time", "WAT": "West Africa Standard Time", "SGT": "Singapore Standard Time", "ACST": "Australian Central Standard Time", "GYT": "Guyana Time", "HKST": "Hong Kong Summer Time", "TMST": "Turkmenistan Summer Time", "ChST": "Chamorro Standard Time", "IST": "India Standard Time", "∅∅∅": "Azores Summer Time", "EST": "Eastern Standard Time", "GFT": "French Guiana Time", "COST": "Colombia Summer Time", "MDT": "Macau Summer Time", "LHST": "Lord Howe Standard Time", "HADT": "Hawaii-Aleutian Daylight Time", "AWST": "Australian Western Standard Time", "NZDT": "New Zealand Daylight Time", "WART": "Western Argentina Standard Time", "WARST": "Western Argentina Summer Time", "JST": "Japan Standard Time", "AKST": "Alaska Standard Time", "AST": "Atlantic Standard Time", "WEZ": "Western European Standard Time", "VET": "Venezuela Time", "ACWDT": "Australian Central Western Daylight Time", "ARST": "Argentina Summer Time", "AWDT": "Australian Western Daylight Time", "PDT": "Pacific Daylight Time", "NZST": "New Zealand Standard Time", "PST": "Pacific Standard Time", "WIT": "Eastern Indonesia Time", "CST": "Central Standard Time", "OEZ": "Eastern European Standard Time", "AEDT": "Australian Eastern Daylight Time", "HNT": "Newfoundland Standard Time", "HAT": "Newfoundland Daylight Time", "CHADT": "Chatham Daylight Time", "WAST": "West Africa Summer Time", "COT": "Colombia Standard Time", "JDT": "Japan Daylight Time", "AKDT": "Alaska Daylight Time"}, + timezones: map[string]string{"JDT": "Japan Daylight Time", "BOT": "Bolivia Time", "∅∅∅": "Acre Summer Time", "SGT": "Singapore Standard Time", "TMST": "Turkmenistan Summer Time", "NZST": "New Zealand Standard Time", "ChST": "Chamorro Standard Time", "AEST": "Australian Eastern Standard Time", "AEDT": "Australian Eastern Daylight Time", "GFT": "French Guiana Time", "ACDT": "Australian Central Daylight Time", "PST": "Pacific Standard Time", "MESZ": "Central European Summer Time", "ACWDT": "Australian Central Western Daylight Time", "ECT": "Ecuador Time", "EST": "Eastern Standard Time", "ADT": "Atlantic Daylight Time", "SAST": "South Africa Standard Time", "UYST": "Uruguay Summer Time", "PDT": "Pacific Daylight Time", "NZDT": "New Zealand Daylight Time", "WARST": "Western Argentina Summer Time", "GMT": "Greenwich Mean Time", "MST": "Macau Standard Time", "ACST": "Australian Central Standard Time", "LHST": "Lord Howe Standard Time", "HNT": "Newfoundland Standard Time", "WIB": "Western Indonesia Time", "HKST": "Hong Kong Summer Time", "WITA": "Central Indonesia Time", "OESZ": "Eastern European Summer Time", "CLST": "Chile Summer Time", "VET": "Venezuela Time", "CAT": "Central Africa Time", "WAST": "West Africa Summer Time", "IST": "India Standard Time", "ARST": "Argentina Summer Time", "CLT": "Chile Standard Time", "COST": "Colombia Summer Time", "HKT": "Hong Kong Standard Time", "LHDT": "Lord Howe Daylight Time", "MYT": "Malaysia Time", "WEZ": "Western European Standard Time", "ART": "Argentina Standard Time", "MDT": "Macau Summer Time", "GYT": "Guyana Time", "AKST": "Alaska Standard Time", "CST": "Central Standard Time", "MEZ": "Central European Standard Time", "ACWST": "Australian Central Western Standard Time", "BT": "Bhutan Time", "EAT": "East Africa Time", "JST": "Japan Standard Time", "HADT": "Hawaii-Aleutian Daylight Time", "AKDT": "Alaska Daylight Time", "WESZ": "Western European Summer Time", "OEZ": "Eastern European Standard Time", "AWST": "Australian Western Standard Time", "AWDT": "Australian Western Daylight Time", "COT": "Colombia Standard Time", "HAST": "Hawaii-Aleutian Standard Time", "WART": "Western Argentina Standard Time", "WAT": "West Africa Standard Time", "UYT": "Uruguay Standard Time", "CHAST": "Chatham Standard Time", "EDT": "Eastern Daylight Time", "AST": "Atlantic Standard Time", "HAT": "Newfoundland Daylight Time", "WIT": "Eastern Indonesia Time", "CHADT": "Chatham Daylight Time", "TMT": "Turkmenistan Standard Time", "SRT": "Suriname Time", "CDT": "Central Daylight Time"}, } } @@ -246,7 +246,7 @@ func (en *en) FmtNumber(num float64, v uint64) string { // NOTE: 'num' passed into FmtPercent is assumed to be in percent already func (en *en) FmtPercent(num float64, v uint64) string { s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) - l := len(s) + len(en.decimal) + l := len(s) + 3 b := make([]byte, 0, l) for i := len(s) - 1; i >= 0; i-- { |