Thread: mingw optimization question

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    24

    mingw optimization question

    Does mingw optimize multiplication/division by one? That is, if I write

    Code:
    ONE * a
    or

    Code:
    a * ONE
    or

    Code:
    a / ONE
    , will it be optimized to just

    Code:
    a
    ? Here, ONE is either a define or a const that is set to one. Also, does it depend on whether a is a float type or not? If a is an integer, ONE is of course also an integer.

    The reason I'm wondering is because I have many expressions that contain a variable that may or may not be set to one, depending on a compile time flag. If the expressions are not optimized when the variable is set to one, I would have to write two different versions of each expression, one for each value (0 or 1) of the flag, and optimize the expressions manually.
    Last edited by then; 12-10-2011 at 05:37 PM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    It seems...(from a single trivial check of the asm output) that gcc does it.
    But that may not be the case for more complicated code.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just write the code and let the compiler sort it out.

    Otherwise, you're just suffering from premature optimisation disease.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Generally, gcc (like a lot of other modern compilers) is able to optimise out such things, particularly at higher optimisation settings. I wouldn't bet on it happening for unoptimised compilation (or when compiling for debugging purposes).

    Logically, however, I would wonder why a programmer would deliberately write code of the form "x = ONE * a" or "y = a/ONE" if you know that ONE expands, at compile time, to a numeric value 1. I am aware that sometimes one as expressions of the form "x = some_configurable_factor * a" or "y = a/some_configurable_factor" where some_configurable_factor is always a compile time constant, but may be configured to different values when building. But few folks who understand the english language would look at a name ONE and expect it to expand to (say) a value of 47.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    24
    Quote Originally Posted by grumpy View Post
    Logically, however, I would wonder why a programmer would deliberately write code of the form "x = ONE * a" or "y = a/ONE" if you know that ONE expands, at compile time, to a numeric value 1.
    It was just a very simple example. In my case the variable is of course not called ONE, but is a parameter used in a simulation program. If SI units are used, this parameter will not be one and it will be stored as a variable; however, if reduced units are used, this parameter will become one and will then either be a define or a const.

    Quote Originally Posted by Salem View Post
    Just write the code and let the compiler sort it out.
    How do I do that; how can I see the assembly code that is generated?

    Quote Originally Posted by Salem View Post
    Otherwise, you're just suffering from premature optimisation disease.
    Haha, that's a good one.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You compile
    gcc -S prog.c
    to get prog.s

    But you're just wasting your time IMO.

    Until you have a finished program, which you can profile with real-world data, you're in no position to even guess where the performance bottlenecks are.

    What you can do however is make sure you're using the most appropriate algorithms and data structures for the problem. Because there isn't a compiler out there that can fix the monumental screw-up of writing say bubble sort when the best thing to do would have been to use quicksort.

    Leave the micro-optimisations and bit fiddling to the compiler (it's good at doing it, and will beat you every time). Focus on the big issues like choosing algorithms, and making sure the damn thing works (nobody will give a damn about your buggy program if it produces garbage, no matter how fast it is).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    24
    Quote Originally Posted by Salem View Post
    What you can do however is make sure you're using the most appropriate algorithms and data structures for the problem. Because there isn't a compiler out there that can fix the monumental screw-up of writing say bubble sort when the best thing to do would have been to use quicksort.

    Leave the micro-optimisations and bit fiddling to the compiler (it's good at doing it, and will beat you every time). Focus on the big issues like choosing algorithms, and making sure the damn thing works (nobody will give a damn about your buggy program if it produces garbage, no matter how fast it is).
    Agreed. You might be especially right with the last part because our program doesn't even work yet! Thanks for getting me on the right track.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about stochastic optimization
    By Chris_1980 in forum C++ Programming
    Replies: 6
    Last Post: 06-26-2011, 02:15 AM
  2. Optimization question
    By Noise in forum C++ Programming
    Replies: 25
    Last Post: 02-14-2009, 01:40 PM
  3. optimization question
    By ole111 in forum C Programming
    Replies: 3
    Last Post: 09-10-2005, 03:42 PM
  4. Optimization Question
    By saxman in forum C Programming
    Replies: 7
    Last Post: 06-30-2004, 10:49 PM
  5. question on optimization
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2002, 11:07 PM

Tags for this Thread