Skip to content

Instantly share code, notes, and snippets.

@Ichoran
Created January 25, 2017 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ichoran/9a2344a5567832423b63ac3989813d4b to your computer and use it in GitHub Desktop.
Save Ichoran/9a2344a5567832423b63ac3989813d4b to your computer and use it in GitHub Desktop.
Quick and dirty benchmark of different approaches to Range specialization
package test
class Super[+A] {
def foreach[U](f: A => U): Unit = ???
}
final class Ranged(i0: Int, iN: Int) extends Super[Int] {
override def foreach[@specialized(Unit) U](f: Int => U) {
var i = i0
while (i < iN) {
f(i)
i += 1
}
}
}
final class Runged(i0: Int, iN: Int) extends Super[Int] {
def foreachIntUnit(f: Int => Unit) {
var i = 0
while (i < iN) {
f(i)
i += 1
}
}
override def foreach[U](f: Int => U) {
if (f.isInstanceOf[scala.runtime.AbstractFunction1$mcVI$sp]) foreachIntUnit(f.asInstanceOf[Int => Unit])
else {
var i = 0
while (i < iN) {
f(i)
i += 1
}
}
}
}
object Bench {
def main(args: Array[String]) {
val th = new ichi.bench.Thyme
println("Ignore a few of these, I didn't warm everything up")
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ var s = 0; var i = 1; while (i <= 1000) { s += i*i; i += 1 }; s }
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ var s = 0; var i = 1; while (i <= 1000) { s += i*i; i += 1 }; s }
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ var s = 0; var i = 1; while (i <= 1000) { s += i*i; i += 1 }; s }
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ var s = 0; var i = 1; while (i <= 1000) { s += i*i; i += 1 }; s }
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ var s = 0; var i = 1; while (i <= 1000) { s += i*i; i += 1 }; s }
println
println("Okay, now maybe the quick and dirty tests will mean something.")
println("While loop")
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ var s = 0; var i = 1; while (i <= 1000) { s += i*i; i += 1 }; s }
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ var s = 0; var i = 1; while (i <= 1000) { s += i*i; i += 1 }; s }
println
println("Ranged")
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ val r = new test.Ranged(1, 1001); var s = 0; r.foreach(i => s += i*i); s }
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ val r = new test.Ranged(1, 1001); var s = 0; r.foreach(i => s += i*i); s }
println
println("Runged")
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ val r = new test.Runged(1, 1001); var s = 0; r.foreach(i => s += i*i); s }
th.pbenchOff(){ var s = 0; for (i <- 1 to 1000) s += i*i; s }{ val r = new test.Runged(1, 1001); var s = 0; r.foreach(i => s += i*i); s }
println
var s = 0
val f: (Int => Unit) = i => { s += i*i }
val f2:(Int => Int) = i => { s += i*i; i }
println
println("Ranged f")
th.pbenchOff(){ s = 0; (1 to 1000).foreach(f); s }{ val r = new test.Ranged(1, 1001); s = 0; r.foreach(f); s }
th.pbenchOff(){ s = 0; (1 to 1000).foreach(f); s }{ val r = new test.Ranged(1, 1001); s = 0; r.foreach(f); s }
println
println("Runged f")
th.pbenchOff(){ s = 0; (1 to 1000).foreach(f); s }{ val r = new test.Runged(1, 1001); s = 0; r.foreach(f); s }
th.pbenchOff(){ s = 0; (1 to 1000).foreach(f); s }{ val r = new test.Runged(1, 1001); s = 0; r.foreach(f); s }
}
}
Ignore a few of these, I didn't warm everything up
Benchmark comparison (in 1.251 s)
Significantly different (p ~= 0)
Time ratio: 0.56578 95% CI 0.55955 - 0.57201 (n=30)
First 566.8 ns 95% CI 563.8 ns - 569.9 ns
Second 320.7 ns 95% CI 317.6 ns - 323.8 ns
Individual benchmarks not fully consistent with head-to-head (p ~= 0)
First 560.9 ns 95% CI 560.5 ns - 561.3 ns
Second 347.3 ns 95% CI 347.1 ns - 347.5 ns
Benchmark comparison (in 558.1 ms)
Significantly different (p ~= 0)
Time ratio: 0.50093 95% CI 0.47807 - 0.52379 (n=20)
First 554.4 ns 95% CI 543.1 ns - 565.7 ns
Second 277.7 ns 95% CI 266.4 ns - 289.1 ns
Individual benchmarks not fully consistent with head-to-head (p ~= 0)
First 312.5 ns 95% CI 311.1 ns - 314.0 ns
Second 304.7 ns 95% CI 304.5 ns - 304.9 ns
Benchmark comparison (in 473.1 ms)
Significantly different (p ~= 0.0020)
Time ratio: 0.97379 95% CI 0.95812 - 0.98946 (n=20)
First 317.6 ns 95% CI 314.1 ns - 321.2 ns
Second 309.3 ns 95% CI 305.7 ns - 312.9 ns
Benchmark comparison (in 474.9 ms)
Significantly different (p ~= 8.825e-08)
Time ratio: 0.96410 95% CI 0.95334 - 0.97486 (n=20)
First 319.2 ns 95% CI 316.8 ns - 321.7 ns
Second 307.8 ns 95% CI 305.3 ns - 310.2 ns
Benchmark comparison (in 475.6 ms)
Significantly different (p ~= 1.878e-05)
Time ratio: 0.96636 95% CI 0.95276 - 0.97997 (n=20)
First 319.4 ns 95% CI 316.3 ns - 322.6 ns
Second 308.7 ns 95% CI 305.6 ns - 311.8 ns
Okay, now maybe the quick and dirty tests will mean something.
While loop
Benchmark comparison (in 472.2 ms)
Significantly different (p ~= 1.992e-07)
Time ratio: 0.96530 95% CI 0.95447 - 0.97613 (n=20)
First 318.7 ns 95% CI 316.2 ns - 321.2 ns
Second 307.6 ns 95% CI 305.1 ns - 310.1 ns
Benchmark comparison (in 473.1 ms)
Significantly different (p ~= 5.946e-07)
Time ratio: 0.96623 95% CI 0.95507 - 0.97738 (n=20)
First 318.2 ns 95% CI 315.7 ns - 320.8 ns
Second 307.5 ns 95% CI 304.9 ns - 310.0 ns
Ranged
Benchmark comparison (in 475.0 ms)
Significantly different (p ~= 3.270e-05)
Time ratio: 0.95943 95% CI 0.94245 - 0.97640 (n=20)
First 320.8 ns 95% CI 316.9 ns - 324.7 ns
Second 307.8 ns 95% CI 303.9 ns - 311.7 ns
Benchmark comparison (in 473.6 ms)
Significantly different (p ~= 2.054e-06)
Time ratio: 0.95900 95% CI 0.94455 - 0.97344 (n=20)
First 319.8 ns 95% CI 316.5 ns - 323.2 ns
Second 306.7 ns 95% CI 303.4 ns - 310.1 ns
Runged
Benchmark comparison (in 475.1 ms)
Significantly different (p ~= 2.171e-08)
Time ratio: 0.96164 95% CI 0.95089 - 0.97239 (n=20)
First 318.7 ns 95% CI 316.3 ns - 321.2 ns
Second 306.5 ns 95% CI 304.0 ns - 309.0 ns
Benchmark comparison (in 480.0 ms)
Significantly different (p ~= 7.151e-05)
Time ratio: 0.96200 95% CI 0.94518 - 0.97883 (n=20)
First 320.0 ns 95% CI 316.1 ns - 323.9 ns
Second 307.9 ns 95% CI 304.0 ns - 311.7 ns
Ranged f
Benchmark comparison (in 493.2 ms)
Not significantly different (p ~= 0.3170)
Time ratio: 1.00557 95% CI 0.99450 - 1.01664 (n=20)
First 322.0 ns 95% CI 319.5 ns - 324.5 ns
Second 323.8 ns 95% CI 321.3 ns - 326.3 ns
Benchmark comparison (in 494.8 ms)
Not significantly different (p ~= 0.4968)
Time ratio: 1.00470 95% CI 0.99089 - 1.01852 (n=20)
First 323.4 ns 95% CI 320.3 ns - 326.6 ns
Second 324.9 ns 95% CI 321.8 ns - 328.1 ns
Runged f
Benchmark comparison (in 501.8 ms)
Not significantly different (p ~= 0.3357)
Time ratio: 1.00642 95% CI 0.99314 - 1.01971 (n=20)
First 323.2 ns 95% CI 320.2 ns - 326.2 ns
Second 325.3 ns 95% CI 322.2 ns - 328.3 ns
Benchmark comparison (in 498.4 ms)
Not significantly different (p ~= 0.2173)
Time ratio: 1.00677 95% CI 0.99588 - 1.01766 (n=20)
First 321.8 ns 95% CI 319.4 ns - 324.3 ns
Second 324.0 ns 95% CI 321.5 ns - 326.5 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment