I agree with what VaTTeRGeR suggested. Definitely reduce the sampling rate at "uninteresting" points.
When you want to go full speed, and get everything out of the JVM/Android VM, read on...
Then I need ~15ms to calculate all new values. My plan is to update the function every 10 ms.
Do you know a simple libary to solve that (speed)-problem?
I have a string with a function and want the result for x >= -500 and x <= 500 in (0.1 steps). At the moment I tried exp4j, javaluator, my own solver and the javax.script.ScriptEngine . And all solutions needs too much time.
On HotSpot/OpenJDK, my plan for this would be:
- build a simple JavaCC or ANTLR grammar to recognize math expressions (for inspiration, look at the JavaCC example grammars
or this stackoverflow thread
- define simple AST classes (Constant, BinaryOp, UnaryOp, FunctionCall, etc.)
- use grammar actions to instantiate the AST
- define a common "MathExpression" interface with an abstract "double evaluate(double x)" method
- Use objectweb's ASM library
to generate a new class that implements MathExpression and evaluate(), by walking the AST and generating bytecode instructions for the math operations
- Define the generated class in the JVM
-- preferably use sun.misc.Unsafe.defineAnonymousClass() when available (>= 1.7) (same is used to generated classes for Java 8 lambdas implementing the functional interface);
-- or use java.lang.ClassLoader.defineClass()
... but on a smartphone it is way too slow (only with the 't' variable).
I tried so much libs and the best for me is the JEP Java library (the last free version). With that the smartphone version is only 5 times slower than it should ... with my solution or other libs the factor is 30 to 100 times slower. And that is frustrating.
For Android Dalvik VM, code generation and defining in the VM looks a bit different. Here you can use:
- objectweb's ASMDEX
library and instantiating a new dalvik.system.DexClassLoader
giving it the generated dex file as first constructor argument
- use Dexmaker
that does the latter of the above for you
What they do is basically generate a new .dex file at runtime and load it into the VM with a new DexClassLoader.
I however did not try the Dalvik code generation approach. It would be very interesting if that would work for you.