Evaluatiing an expression

In maths the expression sqrt((x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1)) can be used to measure the distance between two points at [x0, y0] and [x1, y1]. The following code uses Jasmine to calculate the distance between the points [-5, 4] and [7, 9]

String expr = "sqrt((x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1))";
Expression e = Compile.expression(expr, true);
System.out.println("Build time = " + e.getBuildTime() + " nanoseconds");
float d = e.eval(-5, 7, 4, 9).answer().toFloat();
System.out.println("Distance = " + d);
System.out.println("Eval time = " + e.getEvalTime() + " nanoseconds");

We start with the textual description of the expression we want to evaluate and then in line 2 instruct the library to compile the expression and return an Expression object (e). The second parameter(Boolean) determines whether or not to measure the time it takes to perform evaluations.

Jasmine always measures the time it takes to build an expression and although not needed it is interesting to see so it is printed out in line 3.

In line 4 we use the Expression object to perform the evaluation using the values passed in the parameters. The order of the values passed must match the order they appear in the expression, so in this evaluation

x0 = -5, x1 = 7, y0 = 4 and y1 = 9

After the evaluation we get the answer and convert it to a float before printing it. Finally we print the time it took to perform the evaluation. For simple expressions the time to measure and record the evaluation time is significant compared to the actual time evaluating the expression. If speed is important then use false for the second parameter in the compile method.

Once an expression has been compiled we can perform as many evaluations as we like with different parameter values and without having to recompile.

To perform a second evaluation we can simply add the following lines to the program.

d = e.eval(10, 14, 22, 19).answer().toFloat();
System.out.println("Distance = " + d);
System.out.println("Eval time = " + e.getEvalTime() + " nanoseconds");

On my machine the output from the code above was

Build time = 18756000 nanoseconds
Distance = 13.0
Eval time = 29000 nanoseconds
Distance = 5.0
Eval time = 1000 nanoseconds

A nanosecond is 10-9 or one thousand millionth of a second.

So it took 18.76ms to compile and if you are interested it took 38.28ms to initialize the compiler. The interesting part is the massive difference in time taken over the two evaluations; this is due to the JIT compiler performing runtime optimisations and occurs with standard Java code as well.

Unless we have something to compare these times with then they have little meaning. The following table compares these with performing the same evaluation using standard compiled Java code.

Evaluation Jasmine Java
First 29000 ns 30000 ns
Second 0 (i.e. < 1000) ns 1000 ns

The resolution on my machine is 1000 nanoseconds which explains the zero result.

Notice the comparison to Java, now that's impressive!