discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Internal treatment of common expressions

LB
L Boyd
Thu, Jun 2, 2016 12:27 PM

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

View this message in context: http://forum.openscad.org/Internal-treatment-of-common-expressions-tp17527.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

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 -- View this message in context: http://forum.openscad.org/Internal-treatment-of-common-expressions-tp17527.html Sent from the OpenSCAD mailing list archive at Nabble.com.
TP
Torsten Paul
Thu, Jun 2, 2016 1:13 PM

Von: "L Boyd" lboyd@frontiernet.net

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.

Von: "L Boyd" <lboyd@frontiernet.net> > 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.