Skip to content

Performance for compile and evaluate #3103

Answered by josdejong
sylee957 asked this question in Q&A
Discussion options

You must be logged in to vote

Two things here:

  1. You can try do something like:
    var mathfn = math.evaluate('fn(x, y, z) = x^2 + y^2 + z^2')
    
    And then run fn(x, y, z) in your benchmark
  2. The main reason for the slowness is that for evaluation, mathjs uses a heavy weight functions math.add and math.pow that are capable of handling numbers, BigNumbers, Units, Fractions, Matrices, etc. If you only need numbers, you can speed things up a lot by creating a mathjs instance with lightweight functions. To do that, you can use the plain-number implementation via import {...} from 'mathjs/number' (see docs), or by providing alternative function implementations yourself. Examples:

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by sylee957
Comment options

You must be logged in to vote
4 replies
@josdejong
Comment options

@Yuxin-Lou
Comment options

@josdejong
Comment options

@Yuxin-Lou
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants