Thread: Storing and retrieving vs. recalculating.

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    54

    Storing and retrieving vs. recalculating.

    Hi,

    Can someone shed some light on whether recalculating a value is faster or slower than calculating and then storing and retrieving the value.

    I understand that amount of time the value is needed will have an effect on the answer. So here is an example:

    Code:
    int i, j, tmp;
    
    for (i = 0; i < max; i++)
    {
        for (j = 0; j < max - i; j++)
        {
            if (strcmp(array[j].word, array[j + 1].word) > 0)
            {
                memcpy(&temp, array[j], sizeof(struct _menu_item));
                memcpy(array[j], array[j + 1], sizeof(struct _menu_item));
                memcpy(array[j + 1], &temp, sizeof(struct _menu_item));
            }
        }
    }
    The above code shows that the 'sizeof(struct _menu_item)' is used 3 separate times. In this example, would it be more efficient to calculate the 'sizeof(struct _menu_item)' and store it, and then use the variable instead of the recalculating 2 more times?

    Ty

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    949
    There should be no difference in this case since 'sizeof(struct _menu_item)' is constant, so nothing needs to be "calculated" at run-time since it's done at compile time.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,543
    Since sizeof is a compile time (except when dealing with the horrible VLA) operator it really won't make much difference.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    54
    Quote Originally Posted by john.c View Post
    There should be no difference in this case since 'sizeof(struct _menu_item)' is constant, so nothing needs to be "calculated" at run-time since it's done at compile time.
    So you're saying, unless there's an externally supplied value (user/file input) that gets calculated in the program, all my calculations are constants.

    You kinda blew my mind there! These are the things you miss when you learn a language on your own.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    949
    Quote Originally Posted by Yonut View Post
    So you're saying, unless there's an externally supplied value (user/file input) that gets calculated in the program, all my calculations are constants.
    I wouldn't put it quite like that.
    But if a calculation can be done at compile time then that's when it will usually be done (at least with optimizations).
    So if you say printf("%d\n", 2 * 3 * 4) then that calculation will be done at compile time.
    It would be kind of stupid to calculate it over and over again when it never changes.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    54
    Quote Originally Posted by john.c View Post
    I wouldn't put it quite like that.
    But if a calculation can be done at compile time then that's when it will usually be done (at least with optimizations).
    So if you say printf("%d\n", 2 * 3 * 4) then that calculation will be done at compile time.
    It would be kind of stupid to calculate it over and over again when it never changes.
    I used the wrong wording...

    Any values calculated at compile time are constant values, and values calculated at runtime are dynamic values

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline retrieving an int
    By andirew in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2015, 10:49 AM
  2. Retrieving Data from DLL
    By JJFMJR in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2008, 07:44 AM
  3. Retrieving Images off the Web
    By harry_p in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 12:38 AM
  4. Retrieving Pixels
    By SwitchBladeX in forum Windows Programming
    Replies: 3
    Last Post: 06-03-2002, 05:54 PM

Tags for this Thread