From f42f54af40b37e4d4e1a9f4fe2f4cfee0d6358b6 Mon Sep 17 00:00:00 2001 From: jimi Date: Mon, 7 Mar 2016 15:40:47 -0600 Subject: [PATCH] Refactor with switch, update benchmark stats --- raindrops.go | 32 +++++++++++++++++++------------- switch.bench | 3 +++ 2 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 switch.bench diff --git a/raindrops.go b/raindrops.go index 6c1b6a8..7b10e64 100644 --- a/raindrops.go +++ b/raindrops.go @@ -6,22 +6,28 @@ const testVersion = 2 func Convert(i int) string { var s string - if i%3 == 0 { - s += "Pling" - } - if i%5 == 0 { - s += "Plang" - } - if i%7 == 0 { - s += "Plong" - } - if s == "" { + switch { + case i%3+i%5+i%7 == 0: + s = "PlingPlangPlong" + case i%3+i%5 == 0: + s = "PlingPlang" + case i%3+i%7 == 0: + s = "PlingPlong" + case i%3 == 0: + s = "Pling" + case i%5+i%7 == 0: + s = "PlangPlong" + case i%5 == 0: + s = "Plang" + case i%7 == 0: + s = "Plong" + default: s = strconv.Itoa(i) } return s } -// Iteration 1 Benchmark +// Iteration 2 Benchmark // PASS -// BenchmarkConvert-12 2000000 676 ns/op -// ok exercism/go/raindrops 2.048s +// BenchmarkConvert-12 5000000 309 ns/op +// ok exercism/go/raindrops 1.869s diff --git a/switch.bench b/switch.bench new file mode 100644 index 0000000..569a189 --- /dev/null +++ b/switch.bench @@ -0,0 +1,3 @@ +PASS +BenchmarkConvert-12 5000000 309 ns/op +ok exercism/go/raindrops 1.869s