Scala Results:

Scala Array Test and for loops. n=1.0e+06 completed in 0.961 seconds
Scala Array Test and while loops. n=1.0e+06 completed in 0.130 seconds

This is with Scala 2.10-M7 and JDK 7u7.

First, obvious gotcha. whlie loops run *way* faster than for loops with 
ranges in Scala. I thought they were fixing that optimization issue in 2.10.

Second, Scala Array[Int] took ~1.3x as long as Java int[]. I thought Scala 
was much closer from my older benchmarks. Maybe that's a JDK 7/8 
difference? I don't feel like running that right now.

Code:

package scalatest

object ArrayTest {
def benchmark(name: String, runnable: () => Unit): Unit = {
val start: Long = System.nanoTime();
runnable();
val stop = System.nanoTime();
val delta = stop - start;
val seconds: Double = delta.asInstanceOf[Double] / 1000000000.0;

println("%s completed in %.3f seconds".format(name, seconds));
}

def main(args: Array[String]): Unit = {
val n = 1000000;

testWithScalaArrayAndFor(n);
testWithScalaArrayAndWhile(n);
}

def testWithScalaArrayAndFor(n: Int): Unit = {
benchmark("Scala Array Test and for loops. 
n=%6.1e".format(n.asInstanceOf[Double]), () => {
val a = new Array[Int](n);

// Do something with the array...
for (i <- 0 until n) {
a(i) = i;
}
for (k <- 0 until 100) {
for (i <- 0 until n) {
a(i) = a(n-1-i) * 2;
}
}
});
}
 def testWithScalaArrayAndWhile(n: Int): Unit = {
benchmark("Scala Array Test and while loops. 
n=%6.1e".format(n.asInstanceOf[Double]), () => {
val a = new Array[Int](n);

// Do something with the array...
var i: Int = 0;
while (i < n) {
a(i) = i;
i += 1;
}
var k: Int = 0;
while (k < 100) {
i = 0;
while (i < n) {
a(i) = a(n-1-i) * 2;
i += 1;
}
k += 1;
}
});
} 
}

-- 
You received this message because you are subscribed to the Google Groups "Java 
Posse" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/javaposse/-/iZk80_4l2l0J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to