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