Matrix Multiplication in Scalanlp Breeze completed in zero millisecond

151 views
Skip to first unread message

Chandan Misra

unread,
Oct 7, 2017, 5:47:14 PM10/7/17
to Scala Breeze

I have the following code two multiply two random matrices of type DenseMatrix.

import breeze.linalg._
object multiply {
    def main(args: Array[String]) {
        var size = 128
        var i = 0
        var temp =0
        var normal01 = breeze.stats.distributions.Gaussian(0, 1)
        var mat1 = DenseMatrix.rand(size,size,normal01)
        var mat2 = DenseMatrix.rand(size,size,normal01)
        var start = System.currentTimeMillis()
        var mat3 = mat1*mat2
        var end = System.currentTimeMillis()
        var duration = end-start
        println("Duration: "+duration)
    }
}

Which gives me the following result

Duration: 234

However when I append the same code after above 4 times to see the next durations on different input matrices, it turned out to be

Duration: 188
Duration: 0
Duration: 0
Duration: 16

Kindly let me know, what is the reason behind this?

David Hall

unread,
Oct 7, 2017, 5:52:05 PM10/7/17
to scala-...@googlegroups.com
Profiling JVM code is notoriously difficult and you should probably read up on it if you make a habit of it.

There's a non-trivial startup time to all JVM code because the JIT compiler "warms up" over time, and Breeze has even higher startup time because it has to register a bunch of stuff and try to load native libraries at first use.

In addition, the JVM is kind of magical and can sometimes realize that certain code isn't actually used and just delete it altogether. To make sure that isn't happening, you could apply some short operation (maybe compute the sum?) and print that out along with the duration.

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/cda91cfb-c339-45d6-81dc-ab1e93462142%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages