Let's say I have some (pseudo) code that includes:
for (25 times)
generatePetalPolygon(same values each time);
where generatePetalPolygon() is a module that does about 300 trig
calculations and generates a 2D polygon,
for each call the same values will be used.
Will the module be run once and the result cached, or will it be run each time it is called?
The ultimate output is a DXF or SVG.
On 4/18/2023 3:49 PM, Bryan Lee wrote:
Let's say I have some (pseudo) code that includes:
for (25 times)
generatePetalPolygon(same values each time);
where generatePetalPolygon() is a module that does about 300 trig
calculations and generates a 2D polygon,
for each call the same values will be used.
Will the module be run once and the result cached, or will it be run each time it is called?
The first question is: Do you really care? Could you even tell the
difference?
I just wrote a test program that did (in different iterations) several
variations on the 7500 trig operations that you're talking about. It
ran in about 50ms on my ten-year-old computer that was inexpensive when
I bought it. Some experiments indicate that OpenSCAD on this computer
can do about 350,000 sin() calls per second.
But the answer is no, it's not cached at that level. I can tell because
I have a module that does a thousand sin() calls, the same thousand each
time, and changing the number of times I call it changes the amount of
time it takes.
Theoretically OpenSCAD could determine that a module's result is purely
determined by its inputs, and could cache the results for each set of
inputs. But would that really be a win? Determining the cache key
would not be trivial - it would need to be the arguments plus all of the
$ variables. Would assembling and hashing that key be more expensive
than the call itself? Maybe.
Generally speaking, OpenSCAD execution time per se is not where time is
spent. Time is spent in rendering. Only very math-intensive
operations take any significant amount of time. A few thousand vertices
is not much.
However, if you really wanted to optimize it, you could have a function
that generated the points. Call that function once, and store the
resulting array and use it multiple times.
I recently reworked the BOSL2 screw code and made it 3x faster (to preview)
by changing computations. I also have a wavy box model where the
computations take 45 seconds, but that's because I'm calling the BOSL2
offset() function which has quadratic run time. I think whether
computations make a difference is a complicated matter. The right way to
approach it is to code up your approach some reasonable, simple way, and
then if it's annoyingly slow....think about optimizations. But don't worry
about optimizations up front. A simple list comprehension of a hundred
thousand points will almost always be fast enough. Most of the time your
code will be fast enough just implemented in some obvious way.
On Tue, Apr 18, 2023 at 7:41 PM Jordan Brown openscad@jordan.maileater.net
wrote:
On 4/18/2023 3:49 PM, Bryan Lee wrote:
Let's say I have some (pseudo) code that includes:
for (25 times)
generatePetalPolygon(same values each time);
where generatePetalPolygon() is a module that does about 300 trig
calculations and generates a 2D polygon,
for each call the same values will be used.
Will the module be run once and the result cached, or will it be run each time it is called?
The first question is: Do you really care? Could you even tell the
difference?
I just wrote a test program that did (in different iterations) several
variations on the 7500 trig operations that you're talking about. It ran
in about 50ms on my ten-year-old computer that was inexpensive when I
bought it. Some experiments indicate that OpenSCAD on this computer can do
about 350,000 sin() calls per second.
But the answer is no, it's not cached at that level. I can tell because I
have a module that does a thousand sin() calls, the same thousand each
time, and changing the number of times I call it changes the amount of time
it takes.
Theoretically OpenSCAD could determine that a module's result is purely
determined by its inputs, and could cache the results for each set of
inputs. But would that really be a win? Determining the cache key would
not be trivial - it would need to be the arguments plus all of the $
variables. Would assembling and hashing that key be more expensive than
the call itself? Maybe.
Generally speaking, OpenSCAD execution time per se is not where time is
spent. Time is spent in rendering. Only very math-intensive operations
take any significant amount of time. A few thousand vertices is not much.
However, if you really wanted to optimize it, you could have a function
that generated the points. Call that function once, and store the
resulting array and use it multiple times.
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org