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

  1. #1
    Registered User
    Join Date
    Nov 2008

    Performance comparison - storage vs on-the-fly

    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. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Quote Originally Posted by pollypocket4eva View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Improve Performance of String comparison
    By yagmai in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 12:11 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM