Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
scabug opened this issue Oct 12, 2012 · 6 comments
Closed

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

scabug opened this issue Oct 12, 2012 · 6 comments
Milestone

Comments

@scabug
Copy link

scabug commented Oct 12, 2012

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.

@scabug
Copy link
Author

scabug commented Oct 12, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6521?orig=1
Reporter: @dlwh
Assignee: @JamesIry
Affected Versions: 2.9.2, 2.10.0-M7

@scabug
Copy link
Author

scabug commented Oct 13, 2012

@dlwh said:
Pull request: scala/scala#1496

One line change:

index 92ea5d3..ab303dd 100644
--- a/src/library/scala/collection/immutable/Range.scala
+++ b/src/library/scala/collection/immutable/Range.scala
@@ -78,6 +78,7 @@ extends scala.collection.AbstractSeq[Int]
   final val terminalElement = start + numRangeElements * step
 
   override def last = if (isEmpty) Nil.last else lastElement
+  override def head = if (isEmpty) Nil.head else start
 
   override def min[A1 >: Int](implicit ord: Ordering[A1]): Int =
     if (ord eq Ordering.Int) {

@scabug
Copy link
Author

scabug commented Oct 25, 2012

@adriaanm said:
fixed for 2.11.x
leaving open for backport to 2.10.x

@scabug
Copy link
Author

scabug commented Jan 8, 2013

@adriaanm said:
James, could you look into backporting?

@scabug
Copy link
Author

scabug commented Jan 8, 2013

@JamesIry said:
scala/scala#1866

@scabug scabug closed this as completed Jan 14, 2013
@scabug
Copy link
Author

scabug commented Feb 9, 2013

@adriaanm said (edited on Feb 9, 2013 9:48:35 PM UTC):
Due to binary compatibility reasons, this cannot go into 2.10.1-RC1.

See scala/scala#2103.

@scabug scabug added this to the 2.11.0-M2 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant