codegen
cost
operation evaluation count
Calling Sequence
Parameters
Description
Examples
cost(x1, x2,...,xn)
xk
-
of type algebraic, name = algebraic, array(algebraic)
The cost command is used to compute an operation count for the numerical evaluation of the given arguments. The operation count is expressed as a polynomial in the names additions, multiplications, assignments, divisions, functions, storage, subscripts, integrals, products and assignments with non-negative integer coefficients.
Assignment of positive real values to these global names yields a weighted cost.
Note that the cost used for computing powers is as follows. For an integral power, repeated multiplication is assumed. For a general power it is assumed to be computed using exp and ln.
The command with(codegen,cost) allows the use of the abbreviated form of this command.
with⁡codegen,cost,optimize:
a≔x+x2+x3+x4:
cost⁡a
3⁢additions+6⁢multiplications
a≔convert⁡a,horner:
3⁢multiplications+3⁢additions
Using numeric values to express the relative costs of various operations, you can derive a numerical measure of the cost of an expression or procedure. This can then be used to evaluate whether or not to use an optimized version.
rp≔randpoly⁡x,y,z:
p≔unapply⁡rp,x,y,z:
po1≔optimize⁡p:po2≔optimize⁡p,:-tryhard:
c≔cost⁡p:
c1≔cost⁡po1:
c2≔cost⁡po2:
c−c1;c−c2;c1−c2
4⁢multiplications−5⁢storage−5⁢assignments
8⁢multiplications−2⁢storage−2⁢assignments
3⁢storage+3⁢assignments+4⁢multiplications
V≔storage=2,additions=1,assignments=1,functions=100,multiplications=20:
eval⁡c,V
424
eval⁡c1,V
359
eval⁡c2,V
270
See Also
codegen[optimize]
Download Help Document