Browse Source

Refactor with switch, update benchmark stats

master
jimi 9 years ago
parent
commit
f42f54af40
  1. 32
      raindrops.go
  2. 3
      switch.bench

32
raindrops.go

@ -6,22 +6,28 @@ const testVersion = 2
func Convert(i int) string { func Convert(i int) string {
var s 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) s = strconv.Itoa(i)
} }
return s return s
} }
// Iteration 1 Benchmark
// Iteration 2 Benchmark
// PASS // 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

3
switch.bench

@ -0,0 +1,3 @@
PASS
BenchmarkConvert-12 5000000 309 ns/op
ok exercism/go/raindrops 1.869s
Loading…
Cancel
Save