# Thread: Performance comparison - storage vs on-the-fly

1. ## Performance comparison - storage vs on-the-fly

Hi,
I'm working on some 3D graphics software that requires a draw loop to access a large number of vertices many times. For each vertex component I need to access the following information:

double vertex_originalvalue_x
double vertex_originalvalue_y
double vertex_originalvalue_z

double vertex_transform_x
double vertex_transform_y
double vertex_transform_z

On each time through the draw loop, I need the transformed value of the vertex (for example: transformedX = vertex_originalvalue_x + vertex_transform_x).
My question is whether it is better to store a separate variable in which the transformation computation has already been performed or whether it should be done on the fly.
Does anyone have some rules of thumb for which mathematical operations make things costly enough that storing a precomputed value is worthwhile? Perhaps a single addition operation is quick enough, but if something like a square root operation were required, would it then makes sense to precompute and store?

Thanks. Any advice, opinion, debate on this issue is welcomed.

2. Originally Posted by pollypocket4eva
Perhaps a single addition operation is quick enough, but if something like a square root operation were required, would it then makes sense to precompute and store?
With sqrt(), for sure. Like as in, don't keep referring to sqrt(x) if x has not changed, put it into memory.

Generally, I think the same applies to all the operations that would seen to involve quite a bit of arithmetic (like trig).

sqrt() is so notoriously slow that John Carmack, the guy who wrote Quake, came up with some alternate function that can be used in many circumstances. I've seen it, it is public (you will have to google for that), can't really say any more as math is not a big interest for me.

3. When in doubt, use a profiler! Simple mathematical operations like elementary operations probably won't matter since they're so cheap, but bigger stuff might incur a penalty hit. So, when in doubt, use a profiler. There are free profilers available.