Skip to content

Range.head is 6x slower than Range.start #6521

@scabug

Description

@scabug

consider the following:

object Foo {

  def foo(x: Int) = {
    var i = 0
    var acc = 0L
    val r = (3 until x)
    while(i < x) {
      acc += r.head
      i += 1
    }
    acc
  }

  def callFoo() = {
    val x = foo(100000)
    println(x)
  }
    

  def main(args: Array[String]) {
    val in = System.currentTimeMillis()
    for(i <- 0 until 10000) {
      callFoo()
    }
    println( (System.currentTimeMillis - in))
  }

}

This runs in 4100ms on my machine. If you replace head with "start" it's 690ms

I'm making a pull request. One line change.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions