From 0575a3cfd79578c7511f8ff86682491cf70d26b1 Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Sat, 20 Dec 2014 11:31:21 +0000 Subject: [PATCH] Start with a slice large enough for the minimum version Closes #12 --- semver.go | 2 +- semver_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/semver.go b/semver.go index abb7757..9df310b 100644 --- a/semver.go +++ b/semver.go @@ -30,7 +30,7 @@ type Version struct { // Version to string func (v Version) String() string { - b := make([]byte, 0) + b := make([]byte, 0, 5) b = strconv.AppendUint(b, v.Major, 10) b = append(b, '.') b = strconv.AppendUint(b, v.Minor, 10) diff --git a/semver_test.go b/semver_test.go index 18bb401..90c4545 100644 --- a/semver_test.go +++ b/semver_test.go @@ -301,6 +301,16 @@ func BenchmarkStringSimple(b *testing.B) { } } +func BenchmarkStringLarger(b *testing.B) { + const VERSION = "11.15.2012" + v, _ := New(VERSION) + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + v.String() + } +} + func BenchmarkStringComplex(b *testing.B) { const VERSION = "0.0.1-alpha.preview+123.456" v, _ := New(VERSION)