typelevel / spire

Powerful new number types and numeric abstractions for Scala.
http://typelevel.org/spire/
MIT License
1.76k stars 242 forks source link

cforRange2 performs poorly compared to cfor and cforRange #308

Open PrettyFlower opened 9 years ago

PrettyFlower commented 9 years ago

Just doing some basic micro-benchmark testing, I noticed it takes about 3x as long to execute cforRange2 compared to cfor and cforRange in what I assume would be equivalent code. I used the following code to test this:

val iMax = 100000
val jMax = 100000

import spire.syntax.cfor._
{
    var count = 0
    cfor(0)(_ < iMax, _ + 1) { i =>
        cfor(0)(_ < jMax, _ + 1) { j =>
            if(j % 2 == 0) {
                count += 2
            }
            else {
                count -= 1
            }
        }
    }
    println(s"count: $count")
}

{
    var count = 0
    cforRange(0 until iMax) { i =>
        cforRange(0 until jMax) { j =>
            if(j % 2 == 0) {
                count += 2
            }
            else {
                count -= 1
            }
        }
    }
    println(s"count: $count")
}

{
    var count = 0
    cforRange2(0 until iMax, 0 until jMax) { (i, j) =>
        if(j % 2 == 0) {
            count += 2
        }
        else {
            count -= 1
        }
    }
    println(s"count: $count")
}
non commented 9 years ago

Sorry I haven't gotten to this sooner. I'll try to take a look before the next release. Thanks for reporting!

PrettyFlower commented 9 years ago

No problem, thanks for getting back to me.