Last active
January 4, 2016 08:28
-
-
Save daimatz/8595111 to your computer and use it in GitHub Desktop.
h は遅い
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.mutable.ArrayBuffer | |
import scala.util.Random | |
object Main { | |
def main(args: Array[String]) { | |
val data = testdata() | |
val start = System.currentTimeMillis() | |
val ret = f(data) // g, h | |
val end = System.currentTimeMillis() | |
println(ret) | |
println(end-start) | |
} | |
def testdata(): Seq[(Int, Int)] = { | |
val random = new Random | |
val buf = new ArrayBuffer[(Int, Int)] | |
(0 until 5000000).foreach { _ => | |
buf += ((random.nextInt, random.nextInt)) | |
} | |
buf | |
} | |
def f(data: Seq[(Int, Int)]): Long = { | |
var ret = 0L | |
data.foreach { case (x, y) => | |
ret += x * y | |
} | |
ret | |
} | |
def g(data: Seq[(Int, Int)]): Long = { | |
var ret = 0L | |
data.foreach { xy: (Int, Int) => | |
ret += xy._1 * xy._2 | |
} | |
} | |
val pf: PartialFunction[(Int, Int), Int] = { | |
case (x, y) => x * y | |
} | |
def h(data: Seq[(Int, Int)]): Long = { | |
var ret = 0L | |
data.foreach { xy: (Int, Int) => | |
ret += pf(xy) | |
} | |
ret | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment