Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Solving simple mathematical equation solving speed (http://www.velocityreviews.com/forums/t621464-solving-simple-mathematical-equation-solving-speed.html)

Lionel 06-21-2008 02:28 AM

Solving simple mathematical equation solving speed
 
Hi all,

I have an equation that gets executing many thousands of times by an
algorithm. When I it is written in code the algorithm is quite fast.
However, I want the user to be able to define the equation themselves,
to solve it this way I use java expression parser (JEP).

For a test example, the algorithm using the hard coded version runs in
about 2 seconds, but using JEP, it takes in the order of 50 seconds! The
expression is only parsed once by JEP, but evaluated many times with
different values.

The expression I'm using looks like: A - (B / C) * D

Does anyone know if such a time difference is expected, and secondly, a
possible faster way of doing this?

Thanks

Lionel.

Arne Vajhøj 06-21-2008 02:42 AM

Re: Solving simple mathematical equation solving speed
 
Lionel wrote:
> I have an equation that gets executing many thousands of times by an
> algorithm. When I it is written in code the algorithm is quite fast.
> However, I want the user to be able to define the equation themselves,
> to solve it this way I use java expression parser (JEP).
>
> For a test example, the algorithm using the hard coded version runs in
> about 2 seconds, but using JEP, it takes in the order of 50 seconds! The
> expression is only parsed once by JEP, but evaluated many times with
> different values.
>
> The expression I'm using looks like: A - (B / C) * D
>
> Does anyone know if such a time difference is expected, and secondly, a
> possible faster way of doing this?


Why not compile a Java class & method on the fly and load it ?

Arne

Andrew Thompson 06-21-2008 02:43 AM

Re: Solving simple mathematical equation solving speed
 
On Jun 21, 12:28*pm, Lionel <lione...@gmail.com> wrote:
...
> I have an equation that gets executing many thousands of times by an
> algorithm. When I it is written in code the algorithm is quite fast.
> However, I want the user to be able to define the equation themselves,
> to solve it this way I use java expression parser (JEP).


This one?
<http://www.singularsys.com/jep/>

> For a test example, the algorithm using the hard coded version runs in
> about 2 seconds, but using JEP, it takes in the order of 50 seconds! The
> expression is only parsed once by JEP, but evaluated many times with
> different values.
>
> The expression I'm using looks like: A - (B / C) * D
>
> Does anyone know if such a time difference is expected, and secondly, a
> possible faster way of doing this?


Use the JavaCompiler* to turn those mathematical
expressions into code within your own app.

*
<http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html>

--
Andrew Thompson
http://pscode.org/

Stefan Ram 06-21-2008 03:00 AM

Re: Solving simple mathematical equation solving speed
 
Lionel <lionelv_@gmail.com> writes:
>The expression I'm using looks like: A - (B / C) * D


The following example page shows how to evaluate expressions
with the compiler API.

http://www.purl.org/stefan_ram/pub/e...ions-with-java

(It uses disk-based compilation. There also is an option to
compile in memory, which is not shown on that page.)


Roedy Green 06-21-2008 03:03 AM

Re: Solving simple mathematical equation solving speed
 
On Sat, 21 Jun 2008 12:28:12 +1000, Lionel <lionelv_@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>Does anyone know if such a time difference is expected, and secondly, a
>possible faster way of doing this?


A clever parser would generate byte code. Then it potentially could
run just as fast as hand-coded if you let the JET JIT or Sun HotSpot
at it.

Most likely though it is generating a list of delegate calls, so you
have considerable overhead just stepping from operation to operation.

see http://mindprod.com/jgloss/onthefly.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Andrew Thompson 06-21-2008 06:00 AM

Re: Solving simple mathematical equation solving speed
 
On Jun 21, 1:00*pm, (Stefan Ram) wrote:
...
> * (...There also is an option to
> * compile in memory, which is not shown on that page.)


For 'in memory' compilation, see..
<http://forum.java.sun.com/thread.jspa?threadID=5305018>

(But in case anybody is wonderring. No, it
cannot be achieved within a sandbox, despite
getting the code directly from a text area.)

--
Andrew Thompson
http://pscode.org/

John B. Matthews 06-21-2008 04:20 PM

Re: Solving simple mathematical equation solving speed
 
In article
<040222fa-ad1e-486f-883b-0cd16949bd19@x19g2000prg.googlegroups.com>,
Andrew Thompson <andrewthommo@gmail.com> wrote:

> On Jun 21, 12:28*pm, Lionel <lione...@gmail.com> wrote:
> [...]
> > For a test example, the algorithm using the hard coded version runs in
> > about 2 seconds, but using JEP, it takes in the order of 50 seconds! The
> > expression is only parsed once by JEP, but evaluated many times with
> > different values.
> >
> > The expression I'm using looks like: A - (B / C) * D
> >
> > Does anyone know if such a time difference is expected, and secondly, a
> > possible faster way of doing this?

>
> Use the JavaCompiler* to turn those mathematical
> expressions into code within your own app.
>
> *
> <http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html>


As Andrew & Stefan suggest, using the compiler will definitely be
faster, but version 1.6 is required. If JEP is too slow or you need
backward compatibility or your syntax is unique, writing your own
recursive descent parser isn't hard:

<http://home.woh.rr.com/jbmatthews/java/calc.html>

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Roedy Green 06-21-2008 06:25 PM

Re: Solving simple mathematical equation solving speed
 
On Sat, 21 Jun 2008 18:31:22 +0100, "Daniel Dyer" <"You don't need
it"> wrote, quoted or indirectly quoted someone who said :

>Alternatively, you could use ANTLR (http://www.antlr.og) to generate a
>parser. In fact, the book "The Definitive ANTLR Reference" by Terrence
>Parr has an example of a mathematical expression parser and even extends
>it to compile to Java bytecode using Jasmin
>(http://jasmin.sourceforge.net/). This approach will work without
>requiring the Compiler API from Java 6.


or any of the other parser generators, see
http://mindprod.com/jgloss/parser.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lionel 06-21-2008 11:09 PM

Re: Solving simple mathematical equation solving speed
 
Lionel wrote:
> Hi all,
>
> I have an equation that gets executing many thousands of times by an
> algorithm. When I it is written in code the algorithm is quite fast.
> However, I want the user to be able to define the equation themselves,
> to solve it this way I use java expression parser (JEP).
>
> For a test example, the algorithm using the hard coded version runs in
> about 2 seconds, but using JEP, it takes in the order of 50 seconds! The
> expression is only parsed once by JEP, but evaluated many times with
> different values.
>
> The expression I'm using looks like: A - (B / C) * D
>
> Does anyone know if such a time difference is expected, and secondly, a
> possible faster way of doing this?


Thanks for the numerous responses. I'm stuck on Java 1.5 for now, so it
appears that rules out a couple of suggestions. I was trying to avoid
the parser generator just because it would mean more code for me to
maintain, but perhaps I will have to bite the bullet and go for it. In
fact I would probably go the recursive descent parser if I take this
line anyway.

I guess that parsing it myself means I need a model of an equation. I'm
sure this has been done to death, so does anyone know of an example
model out there?

Thanks

Lionel.

Roedy Green 06-22-2008 04:05 AM

Re: Solving simple mathematical equation solving speed
 
On Sun, 22 Jun 2008 09:09:57 +1000, Lionel <lionelv_@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>. I was trying to avoid
>the parser generator just because it would mean more code for me to
>maintain, but perhaps I will have to bite the bullet and go for it. In
>fact I would probably go the recursive descent parser if I take this
>line anyway.


Don't be frightened. It is duck simple. You will probably find a
grammar already suitable for your equations. It will be a lot more
work to write your own parser.

Hint, if you write your own, read the Aho and Ulman Dragon book then
use recursion. I wrote a parser for Algol-68 declarations back in my
student day in PL/I. It is one of my great life achievements. No
other student was able to do it. I basked in the approval of the
Professor Peck, one of the designers of Algol 68.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


All times are GMT. The time now is 08:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57