0

I'm developing some modelling software in C# which relies heavily on compiled mathematical statements. They are built at runtime using Linq Expressions giving me native performance. Performance is critical as formulas are run billions of times.

Longer term I'm considering moving the project to Java. However Java doesn't seem to have an equivalent library.

What options can the Java platform provide for compiling mathematical statements at run-time & getting native performance?

(P.S. apparently Java 8 will support lambda expressions, but I doubt the framework be as advanced as Linq.Expressions)

Brendan Hill
  • 3,406
  • 4
  • 32
  • 61
  • I don't think you'll find a 1-to-1 equivalent (at least not that I'm familiar with). Java has its advantages and ways of doing things. You can get amazing performance with java as well, but not in the same ways. You'll have to study the framework, like you would with every technology – Avi Mar 03 '14 at 21:15
  • 1
    The JVM JIT and Hotspot should take any heavily used code and re-compile it into native code at runtime so you should eventually get the same performance. However you would have to actually try it out to see how fast it gets. There's also always JNI to communicate directly with native C code although that is not as portable. – dkatzel Mar 03 '14 at 21:16

1 Answers1

1

One way to do this is to compile a Java class for each different expression, with a single static method that implements the function. See this question.

So, for example, if all your functions just use one variable x, then you could generate a .java file looking like this...

class Expression {
   static double apply(double x) {
       return /* expression here */;
   }
}

Then compile and load that class as in the cited question, get the apply() method with reflection, and call it whenever you want to compute that expression. (Alternately, have your compiled classes implement an interface, create instances of them, and avoid the reflection)

Admittedly, this is not particularly friendly to your memory usage if you end up with thousands of such classes...

Community
  • 1
  • 1
Russell Zahniser
  • 16,188
  • 39
  • 30
  • The security vulnerabilities with taking arbitrary user-provided code and executing it within the scope of your application are...significant. – Servy Mar 03 '14 at 21:26
  • @Servy: I'm assuming here that the code is probably only allowed to contain digits, arithmetic expressions, and 'x' (plus perhaps certain `Math` methods). But actually you can allow arbitrary code if you load it from a class loader unrelated to the rest of the app. – Russell Zahniser Mar 03 '14 at 21:31
  • The equations are parsed by the software first & then converted to expressions/compiled so no security in this case. But it's potential with this approach sure. – Brendan Hill Mar 03 '14 at 21:56
  • Can the Java runtime environment do this compilation? I envisage an online Java applet running in a web browser which needs to compile & run locally, rather than invoking something on the server. – Brendan Hill Mar 03 '14 at 21:57