Skip to content

Instantly share code, notes, and snippets.

@taisukeoe
Created April 20, 2015 14:21
Show Gist options
  • Save taisukeoe/339af4b225eeb3d329cc to your computer and use it in GitHub Desktop.
Save taisukeoe/339af4b225eeb3d329cc to your computer and use it in GitHub Desktop.
FizzBuzzのbench
jmhSettings
[info] # Run complete. Total time: 00:00:38
[info]
[info] Benchmark Mode Cnt Score Error Units
[info] FizzBuzz.fizzBuzzHundred thrpt 3 361575.907 ± 366489.672 ops/s
[info] FizzBuzz.fizzBuzzTenThousand thrpt 3 2987.468 ± 2294.886 ops/s
[info] FizzBuzz.fizzBuzzThousand thrpt 3 32835.541 ± 19145.362 ops/s
[info] FizzBuzz.fizzBuzzTupleHundred thrpt 3 345857.262 ± 42335.462 ops/s
[info] FizzBuzz.fizzBuzzTupleTenThousand thrpt 3 1935.980 ± 4158.743 ops/s
[info] FizzBuzz.fizzBuzzTupleThousand thrpt 3 24860.685 ± 108578.332 ops/s
[success] Total time: 44 s, completed 2015/04/20 23:19:18
package training.fifth
import org.openjdk.jmh.annotations.Benchmark
class FizzBuzz{
@Benchmark
def fizzBuzzTupleHundred():Seq[String] = fizzBuzzTuple(100)
@Benchmark
def fizzBuzzTupleThousand():Seq[String] = fizzBuzzTuple(1000)
@Benchmark
def fizzBuzzTupleTenThousand():Seq[String] = fizzBuzzTuple(10000)
@Benchmark
def fizzBuzzHundred():Seq[String] = fizzBuzz(100)
@Benchmark
def fizzBuzzThousand():Seq[String] = fizzBuzz(1000)
@Benchmark
def fizzBuzzTenThousand():Seq[String] = fizzBuzz(10000)
def fizzBuzzTuple(arg: Int):Seq[String] =
for (i <- 1 to arg) yield
i % 3 -> i % 5 match {
case (0, 0) => "FizzBuzz"
case (0, _) => "Fizz"
case (_, 0) => "Buzz"
case _ => i.toString
}
def fizzBuzz(max:Int):Seq[String] =
for (x <- 1 to max) yield
x match{
case _ if x % 15 == 0 => "FizzBuzz"
case _ if x % 3 == 0 => "Fizz"
case _ if x % 5 == 0 => "Buzz"
case _ => x.toString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment