Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Created August 29, 2014 20:06
Show Gist options
  • Save erikerlandson/2a7ffc767d117ab7d07a to your computer and use it in GitHub Desktop.
Save erikerlandson/2a7ffc767d117ab7d07a to your computer and use it in GitHub Desktop.
Some benchmarking tests to localize differences between "drop sampling" and "filter sampling"
import scala.util.Random
def benchmark_drop(n: Int = 1000, m: Int = 1000, k: Int = 10) {
val rng = new Random()
def dropIter[T](d: Iterator[T]) {
var dd = d
while (! dd.isEmpty) dd = dd.drop(rng.nextInt())
}
var data:Seq[Int] = (1 to m).toArray.toSeq
println("\nArray")
var start = System.currentTimeMillis()
(1 to n).foreach { _ => {
dropIter(data.iterator)
}}
println(s" Time iterator: ${System.currentTimeMillis() - start}")
data = (1 to m).toList
println("\nList")
start = System.currentTimeMillis()
(1 to n).foreach { _ => {
dropIter(data.iterator)
}}
println(s" Time iterator: ${System.currentTimeMillis() - start}")
}
def benchmark_random_log(n: Int = 1000) {
var start = System.currentTimeMillis()
(1 to n).foreach { _ => {
Math.random()
}}
println(s"random: ${System.currentTimeMillis() - start}")
start = System.currentTimeMillis()
(1 to n).foreach { _ => {
Math.log(Math.random())
}}
println(s"log random: ${System.currentTimeMillis() - start}")
val ln2 = Math.log(2.0)
start = System.currentTimeMillis()
(1 to n).foreach { _ => {
Math.log(Math.random())/ln2
}}
println(s"log random div: ${System.currentTimeMillis() - start}")
start = System.currentTimeMillis()
(1 to n).foreach { _ => {
(Math.log(Math.random())/ln2).toInt
}}
println(s"log random div int: ${System.currentTimeMillis() - start}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment