new Jasmine library fast?

edited December 2014 in Library Questions

I was trying the new library Jasmine, which does very fast evaluator for numerical expressions and algorithms. So I tried to setup a small test in order to see if its actually faster (see below). my result however is that just p5 is always faster. Am I doing something wrong or whats going on?

import org.quark.jasmine.*;

void setup() {
String expr = "sqrt((x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1))";
Expression e = Compile.expression(expr, true);

int times = 500000;
float[][] numbers = new float[times][4];
long starttime;

for(int i=0; i < times;i++) {
  for(int n=0; n < 4;n++)
  numbers[i][n] = random(-50,50);
}
float d;
starttime = millis();  
for(int i=0; i < times;i++)
  d = e.eval(numbers[i][0],numbers[i][1],numbers[i][2],numbers[i][3]).answer().toFloat(); 
println("time jasmine: "+(millis()-starttime) + "  millis");

starttime = millis();  
for(int i=0; i < times;i++) 
   d = dist(numbers[i][0],numbers[i][1],numbers[i][2],numbers[i][3]);
println("time p5: "+(millis()-starttime) + "  millis");
}

Answers

  • I have just this moment announced this library so you beat me to it.

    The tests I did were in Eclipse but that should not have made much difference. The results I got are shown on this webpage. If might seem strange but the fact is that the ASM library showed that Jasmine produces more 'compact' Java byte code than Javac (for expressions).

    In line 5 you need to change the true to false that will halve the time taken by Jasmine. If you use true, Jasmine will add extra byte code to internally measure how long the evaluation took in nanoseconds. Although interesting to know it takes a significant amount of time to be executed. For 'production' code use false.

    There are many factors that can affect how long a piece of code takes to execute, not least the run-time code optimizations done by the JVM.

    Whatever the result Jasmine is extremely fast so can be used in many time critical applications.

  • hey, sorry results are more ore less the same here

    import org.quark.jasmine.*;
    
    void setup() {
    String expr = "sqrt((x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1))";
    
    int times = 500000;
    float[][] numbers = new float[times][4];
    long starttime;
    
    for(int i=0; i < times;i++) {
      for(int n=0; n < 4;n++)
      numbers[i][n] = random(-50,50);
    }
    float d;
    Expression e = Compile.expression(expr, false);
    starttime = millis();  
    for(int i=0; i < times;i++)
      d = e.eval(numbers[i][0],numbers[i][1],numbers[i][2],numbers[i][3]).answer().toFloat(); 
    println("time jasmine 1: "+(millis()-starttime) + "  millis");
    
    Expression e2 = Compile.expression(expr, true);
    starttime = millis();  
    for(int i=0; i < times;i++)
      d = e2.eval(numbers[i][0],numbers[i][1],numbers[i][2],numbers[i][3]).answer().toFloat(); 
    println("time jasmine 2: "+(millis()-starttime) + "  millis");
    
    
    starttime = millis();  
    for(int i=0; i < times;i++) 
       d = dist(numbers[i][0],numbers[i][1],numbers[i][2],numbers[i][3]);
    println("time p5: "+(millis()-starttime) + "  millis");
    }
    

    The output I get is: time jasmine 1: 149 millis time jasmine 2: 157 millis time p5: 30 millis

  • On my computer with Processing I got

    time jasmine 1: 44  millis
    time jasmine 2: 70  millis
    time p5: 12  millis
    

    On my computer with Eclipse I got

    time jasmine 1: 25  millis
    time jasmine 2: 58  millis
    time p5: 11  millis
    

    As I said before there are many factors affecting the results. I think in this case it is because the parameters are array elements rather than constants I used in my example on the website.

    Whatever the reason I think the performance of Jasmine for evaluating expressions at run-time is impressive. :D

  • Side note: I don't think Jasmine should be used in place of regular Java formulae, in the hope it will be faster. Most of the time, it won't worth the hassle: it is more cumbersome you loose some type check / compiler check, etc.

    The strength of Jasmine is probably in allowing to handle user input of formulae, interactively or from files, for example. You can find lot of such expression parsers evaluators, and we presented in this forum a simple way (no libraries), using JavaScript evaluation. But the latter is probably much slower than Jasmine.

  • My "Java Mode" example relies on Java's ScriptEngine in order to eval() String trig functions: *-:)
    http://forum.processing.org/two/discussion/7147/some-simple-pulse-equations-using-trig-#Item_9

    There are also P5.JS & CoffeeScript versions of it too down below! =:)

  • Jasmine is not intended to replace Java - it is to evaluate expressions and algorithms at runtime. The fact that under some circumstaces it beats byte code produced by the Java is just luck not design.

Sign In or Register to comment.