Results 1 to 3 of 3

Thread: Using math expressions in C

  1. #1
    Join Date
    Mar 2010
    Location
    France
    Beans
    30
    Distro
    Ubuntu 9.10 Karmic Koala

    Using math expressions in C

    I am wondering which is the preffered way of programming in C, if one is using the same math expressions very often
    1. Use the math function/expression each time
    2. Precalculate the expression and store it to a variable and use the variable instead

    My questions are:
    1. Will there be any difference in the execution time?
    2. When a call is made to a variable to which the expression is stored, is it actually calculating the expression or just retrieving the value of that expression?

    I hope the questions are clear.

    Thanks.

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Using math expressions in C

    The answers to your questions depend on the compiler used, and probably also the optimisation settings. Storing the result in a variable seems to be the best way to ensure the best performance, but it might hurt readability, especially if you name your variables poorly. Personally I would do it only if performance is critical, and even then only if it does indeed make a difference on the compiler used.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Nov 2013
    Beans
    4

    Re: Using math expressions in C

    Answers to your questions
    1. Yes
    2. Just retrieving the value.

    If you evaluate teh same expression often, you should make it a function ie
    x= some expression;
    y= some expression;
    z = some expression;
    the someexpression should be a function:
    double f(double a, double b)
    {
    ..
    return result;
    }
    Since then you can
    1. Do less typing
    2. Test easier
    3. Implement in a better way with little change.
    For more effciency, uses memoization. You need to Google that. Basically whne you caluculate teh function, you store teh result as well. On a future call you look it up, not repeat teh calculation.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •