Internal treatment of common expressions

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Internal treatment of common expressions

L Boyd
A portion of my code, used many times, has the following segment:
        polyhedron(
          points = [
                [cos(as)*Rroot, sin(as)*Rroot, z],    //0
                [cos(as)*Rtipp, sin(as)*Rtipp, z+z1], //1
                [cos(as)*Rroot, sin(as)*Rroot, z+z2], //2
                [cos(ae)*Rroot, sin(ae)*Rroot, z+zd], //3
                [cos(ae)*Rtipp, sin(ae)*Rtipp, z+zd+z1], //4
                [cos(ae)*Rroot, sin(ae)*Rroot, z+zd+z2]], //5

There a 2 values of sin and 2 of cos, each of which is used 3 times.

My question is for someone familiar with the OpenSCAD internals.

Does it calculate each of these 3 times?  If so, is there a performance advantage to assigning them to variables, thus calculating them only once?
Larry
tp3
Reply | Threaded
Open this post in threaded view
|

Re: Internal treatment of common expressions

tp3
Von: "L Boyd" <[hidden email]>

> polyhedron(
> points = [
> [cos(as)*Rroot, sin(as)*Rroot, z], //0
> [cos(as)*Rtipp, sin(as)*Rtipp, z+z1], //1
> [cos(as)*Rroot, sin(as)*Rroot, z+z2], //2
> [cos(ae)*Rroot, sin(ae)*Rroot, z+zd], //3
> [cos(ae)*Rtipp, sin(ae)*Rtipp, z+zd+z1], //4
> [cos(ae)*Rroot, sin(ae)*Rroot, z+zd+z2]], //5
>
> Does it calculate each of these 3 times?
>
Yes

> If so, is there a performance advantage to assigning
> them to variables, thus calculating them only once?
>
Hard to say, probably not for sin() and cos(). In case
it would call a complicated user defined function, the
pre-calculation might help a bit.

In general any simple evaluation tends to be orders
of magnitude faster than the actual mesh calculation,
or even the preview, so there's not much to gain for
the overall time needed.

ciao,
  Torsten.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten