Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
quasiquote vs janino
package org.apache.spark.sql.catalyst.expressions.codegen
import org.codehaus.janino.SimpleCompiler
object CodeGenBenchmark {
def quasiquotes(): Unit = {
import scala.reflect.runtime.{universe => ru}
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
val toolBox = runtimeMirror(this.getClass.getClassLoader).mkToolBox()
var sum = 0
for (i <- 0 to 50000) {
val p0 = System.nanoTime()
val code =
q"""
() => 1 + 1 + "134234".toInt * 980 / 50L.toInt
"""
sum += toolBox.eval(code).asInstanceOf[() => Int].apply()
val p1 = System.nanoTime()
if (i < 10 || i % 1000 == 0) {
println(s"$i: p1 - p0 = ${(p1 - p0)}ns")
}
}
println(sum)
}
def janino(): Unit = {
var sum = 0
for (i <- 0 to 50000) {
val p0 = System.nanoTime()
val compiler = new SimpleCompiler()
compiler.setParentClassLoader(Thread.currentThread().getContextClassLoader)
compiler.cook(
"""
|public class MyClass extends org.apache.spark.sql.catalyst.expressions.codegen.SomeInterface {
| public int eval() {
| return 1 + 1 + Integer.parseInt("134234") * 980 / ((int)50L);
| }
|}
""".stripMargin)
sum += compiler.getClassLoader.loadClass("MyClass").newInstance().asInstanceOf[SomeInterface].eval()
val p1 = System.nanoTime()
//println(s"eval result = $res")
if (i < 10 || i % 1000 == 0) {
println(s"$i: p1 - p0 = ${(p1 - p0)}ns")
}
}
println(sum)
}
def main(args: Array[String]): Unit = {
janino()
quasiquotes()
}
}
abstract class SomeInterface {
def eval(): Int
}
[info] 0: p1 - p0 = 274904000ns
[info] 1: p1 - p0 = 3394000ns
[info] 2: p1 - p0 = 3220000ns
[info] 3: p1 - p0 = 3633000ns
[info] 4: p1 - p0 = 3117000ns
[info] 5: p1 - p0 = 2814000ns
[info] 6: p1 - p0 = 2778000ns
[info] 7: p1 - p0 = 2731000ns
[info] 8: p1 - p0 = 3001000ns
[info] 9: p1 - p0 = 2991000ns
[info] 1000: p1 - p0 = 794000ns
[info] 2000: p1 - p0 = 733000ns
[info] 3000: p1 - p0 = 671000ns
[info] 4000: p1 - p0 = 556000ns
[info] 5000: p1 - p0 = 1008000ns
[info] 6000: p1 - p0 = 611000ns
[info] 7000: p1 - p0 = 532000ns
[info] 8000: p1 - p0 = 519000ns
[info] 9000: p1 - p0 = 551000ns
[info] 10000: p1 - p0 = 816000ns
[info] 11000: p1 - p0 = 483000ns
[info] 12000: p1 - p0 = 536000ns
[info] 13000: p1 - p0 = 546000ns
[info] 14000: p1 - p0 = 469000ns
[info] 15000: p1 - p0 = 460000ns
[info] 16000: p1 - p0 = 501000ns
[info] 17000: p1 - p0 = 548000ns
[info] 18000: p1 - p0 = 481000ns
[info] 19000: p1 - p0 = 516000ns
[info] 20000: p1 - p0 = 731000ns
[info] 21000: p1 - p0 = 566000ns
[info] 22000: p1 - p0 = 565000ns
[info] 23000: p1 - p0 = 539000ns
[info] 24000: p1 - p0 = 623000ns
[info] 25000: p1 - p0 = 533000ns
[info] 26000: p1 - p0 = 556000ns
[info] 27000: p1 - p0 = 657000ns
[info] 28000: p1 - p0 = 674000ns
[info] 29000: p1 - p0 = 791000ns
[info] 30000: p1 - p0 = 581000ns
[info] 31000: p1 - p0 = 720000ns
[info] 32000: p1 - p0 = 606000ns
[info] 33000: p1 - p0 = 758000ns
[info] 34000: p1 - p0 = 662000ns
[info] 35000: p1 - p0 = 650000ns
[info] 36000: p1 - p0 = 930000ns
[info] 37000: p1 - p0 = 813000ns
[info] 38000: p1 - p0 = 623000ns
[info] 39000: p1 - p0 = 932000ns
[info] 40000: p1 - p0 = 874000ns
[info] 41000: p1 - p0 = 711000ns
[info] 42000: p1 - p0 = 730000ns
[info] 43000: p1 - p0 = 833000ns
[info] 44000: p1 - p0 = 681000ns
[info] 45000: p1 - p0 = 1132000ns
[info] 46000: p1 - p0 = 879000ns
[info] 47000: p1 - p0 = 817000ns
[info] 48000: p1 - p0 = 761000ns
[info] 49000: p1 - p0 = 787000ns
[info] 50000: p1 - p0 = 958000ns
[info] -1591955188
[info] 0: p1 - p0 = 2002995000ns
[info] 1: p1 - p0 = 130022000ns
[info] 2: p1 - p0 = 98945000ns
[info] 3: p1 - p0 = 89676000ns
[info] 4: p1 - p0 = 79694000ns
[info] 5: p1 - p0 = 72744000ns
[info] 6: p1 - p0 = 76419000ns
[info] 7: p1 - p0 = 72102000ns
[info] 8: p1 - p0 = 68953000ns
[info] 9: p1 - p0 = 65777000ns
[info] 1000: p1 - p0 = 14397000ns
[info] 2000: p1 - p0 = 13990000ns
[info] 3000: p1 - p0 = 14841000ns
[info] 4000: p1 - p0 = 16311000ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment