Slashdot Mirror


Optimizations - Programmer vs. Compiler?

Saravana Kannan asks: "I have been coding in C for a while (10 yrs or so) and tend to use short code snippets. As a simple example, take 'if (!ptr)' instead of 'if (ptr==NULL)'. The reason someone might use the former code snippet is because they believe it would result in smaller machine code if the compiler does not do optimizations or is not smart enough to optimize the particular code snippet. IMHO the latter code snippet is clearer than the former, and I would use it in my code if I know for sure that the compiler will optimize it and produce machine code equivalent to the former code snippet. The previous example was easy. What about code that is more complex? Now that compilers have matured over years and have had many improvements, I ask the Slashdot crowd, what they believe the compiler can be trusted to optimize and what must be hand optimized?" "How would your answer differ (in terms of the level of trust on the compiler) if I'm talking about compilers for Desktops vs. Embedded systems? Compilers for which of the following platforms do you think is more optimized at present - Desktops (because is more commonly used) or Embedded systems (because of need for maximum optimization)? Would be better if you could stick to free (as in beer) and Open Source compilers. Give examples of code optimizations that you think the compiler can/can't be trusted to do."

2 of 1,422 comments (clear)

  1. Re:Optimization rules... by hesiod · · Score: 0, Troll

    Don't take this the wrong way, but it doesn't sound like you "wrote" a ray tracer at all. You slapped a front end on an existing one, no?

    Granted, there may be subtle differences I'm unaware of because I've never used it...

  2. code monkeys by dutky · · Score: 1, Troll
    Saravana Kannan wrote:
    As a simple example, take 'if (!ptr)' instead of 'if (ptr==NULL)'. The reason someone might use the former code snippet is because they believe it would result in smaller machine code

    Anyone who thinks this doesn't deserve the title Programmer. Any C compiler written in the past 25 years should emit exactly the same code for if(!x) as for if(x==NULL), regardless of optimization. The test in the if statement has an implicit meaning which is generially identical to a comparison to zero or NULL. Anyone who doesn't know this is a moron, especially if they still don't know this after writing C code for 10 years.

    In general, any programmer optimization below the level of selecting an appropriate algorithm is a waste of time. In most cases, anything you can think to do with a variable or expression will be caught by the simplest optimizers out there. In some cases, things that you think will make things faster will actaully slow things down. In all cases, optimizations in the source will make the code harder to read and maintain, which will cost orders of magnitude more (in wasted programmer time) than the savings (in machine cycles) will deliver.

    (I'm tempted to make some racist crack about the submitter, but I've met lots of programmers, from every corner of the planet, with similarly dimwitted notions about what happens to their source code when it is submitted to the compiler. Obviously, the general quality of computer science education (or computer science students) is depressingly low the world over.)