Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Last active August 29, 2015 14:05
Show Gist options
  • Save erikerlandson/488275019e4b54f5cdaf to your computer and use it in GitHub Desktop.
Save erikerlandson/488275019e4b54f5cdaf to your computer and use it in GitHub Desktop.
test performance of breeze abs(v) vs v.map(Math.abs(_))
import breeze.linalg.{Vector => BV, DenseVector => DBV}
import breeze.numerics.abs
def absTest(n: Int = 1000, m: Int = 10) {
var start = System.currentTimeMillis()
val vectors = (1 to n).toArray.map(i => DBV((1 to m).toArray.map(j => -1.0 + (2.0 * Math.random()))))
var end = System.currentTimeMillis()
println(s"Time for Creating: ${end - start}")
start = System.currentTimeMillis()
vectors.map(v => abs(v))
end = System.currentTimeMillis()
println(s"Time for mapping breeze.numerics.abs: ${end - start}")
start = System.currentTimeMillis()
vectors.map(v => v.map(x => Math.abs(x)))
end = System.currentTimeMillis()
println(s"Time for mapping Math.abs: ${end - start}")
}
scala> absTest(n = 1000000, m = 10)
Time for Creating: 1471
Time for mapping breeze.numerics.abs: 622
Time for mapping Math.abs: 1255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment