Like Tree2Likes
  • 2 Post By Salem

mingw optimization question

This is a discussion on mingw optimization question within the C++ Programming forums, part of the General Programming Boards category; Does mingw optimize multiplication/division by one? That is, if I write Code: ONE * a or Code: a * ONE ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,625
    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, Sunshine, 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
    32,823
    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).
    jimblumberg and Elkvis like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 03: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, 04:42 PM
  4. Optimization Question
    By saxman in forum C Programming
    Replies: 7
    Last Post: 06-30-2004, 11:49 PM
  5. question on optimization
    By ygfperson in forum C++ Programming
    Replies: 4
    Last Post: 06-10-2002, 12:07 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21