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."

29 of 1,422 comments (clear)

  1. Ask the compiler... by inertia187 · · Score: 5, Funny

    Programmer: Hey, compiler. How do you like optimizing?
    Compiler: Optimizing? Optimizing? Don't talk to me about optimizing. Here I am, brain the size of a planet, and they've got me optimizing inane snippets of code. Just when you think code couldn't possibly get any worse, it suddenly does. Oh look, a null pointer. I suppose you'll want to see the assembly now. Do you want me to go into an infinite loop or throw an exception right where I'm standing?
    Programmer: Yeah, just show me the stack trace, won't you compiler?

    --
    A programmer is a machine for converting coffee into code.
    1. Re:Ask the compiler... by llamalicious · · Score: 5, Funny

      And then the compiler shared his view of the universe with the programmer, who promptly committed suicide.

    2. Re:Ask the compiler... by saden1 · · Score: 2, Funny

      HG2G has got to be the best book on the planet. Here is my favorite dialog in the book.

      --

      -----
      One is born into aristocracy, but mediocrity can only be achieved through hard work.
    3. Re:Ask the compiler... by edf825 · · Score: 3, Funny

      http://www.in.com.au/missing.html ...I just had to post that :)

  2. You should always... by Anonymous Coward · · Score: 5, Funny

    Optimize. Using cryptic, short variable names also shaves valuable microseconds off compile time and run time.

    1. Re:You should always... by FyRE666 · · Score: 4, Funny

      ... and by god don't let me see anyone using comments - comments are the devil's alphabet soup! Every programmer worth his/her salt knows that source code is self documenting...

    2. Re:You should always... by MillionthMonkey · · Score: 4, Funny

      But the code compiles so much faster when you turn it all into comments.

    3. Re:You should always... by holle2 · · Score: 3, Funny
      Found on The Daily WTF http://thedailywtf.com/ShowPost.aspx?PostID=30233
      ------------
      [...]
      $result = mysql_query("SELECT a, b, c, d, e, f, g, h, i, j, k, l,
      m, n, o, p, q, r, s, t, u, v, w, x, y, z, a2, b2, c2, d2, e2, f2, g2,
      h2, i2, j2, k2, l2, m2, n2, o2, p2, q2, r2, s2, t2, u2, v2, w2, x2, y2, z2,
      a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, n3, o3, p3, q3, r3, s3,
      t3, u3, v3, w3, x3, y3, z3, a4, b4, c4, d4, e4, f4, g4, h4, i4, j4, k4, l4,
      m4, n4, o4, p4, q4, r4, s4, t4, u4, v4, w4, x4, y4, z4, a5, b5, c5, d5, e5,
      f5, g5, h5, i5, j5, k5, l5, m5, n5, o5, p5, q5, r5, s5, t5, u5, v5, w5, x5, y5, z5,
      a6, b6, c6, d6, e6, f6, g6, h6, i6, j6, k6, l6, m6, n6, o6, p6, q6, r6, s6, t6, u6, v6, w6, x6, y6, z6,
      a7, b7, c7, d7, e7, f7, g7, h7, i7, j7, k7, l7, m7, n7, o7, p7, q7, r7, s7, t7, u7, v7, w7, x7, y7, z7,
      a8, b8, c8, d8, e8, f8, g8, h8, i8, j8, k8, l8, m8, n8, o8, p8, q8, r8, s8, t8, u8, v8, w8, x8, y8, z8,
      a9, b9, c9, d9, e9, f9, g9, h9, i9, j9, j9, casenum, contype
      FROM gct WHERE casenum = '$aid'");
      while(list($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,
      $m,$n,$o,$p,$q,$r,$s,$t,$u,$v,$w,$x,$y,$z,$a2,$b2, $c2,$d2,$e2,$f2,$g2,
      $h2,$i2,$j2,$k2,$l2,$m2,$n2,$o2,$p2,$q2,$r2,$s2,$t 2,$u2,$v2,$w2,$x2,$y2,$z2,
      $a3,$b3,$c3,$d3,$e3,$f3,$g3,$h3,$i3,$j3,$k3,$l3,$m 3,$n3,$o3,$p3,$q3,$r3,$s3,
      $t3,$u3,$v3,$w3,$x3,$y3,$z3,$a4,$b4,$c4,$d4,$e4,$f 4,$g4,$h4,$i4,$j4,$k4,$l4,
      $m4,$n4,$o4,$p4,$q4,$r4,$s4,$t4,$u4,$v4,$w4,$x4,$y 4,$z4,$a5,$b5,$c5,$d5,$e5,
      $f5,$g5,$h5,$i5,$j5,$k5,$l5,$m5,$n5,$o5,$p5,$q5,$r 5,$s5,$t5,$u5,$v5,$w5,$x5,
      $y5,$z5,$a6,$b6,$c6,$d6,$e6,$f6,$g6,$h6,$i6,$j6,$k 6,$l6,$m6,$n6,$o6,$p6,$q6,
      $r6,$s6,$t6,$u6,$v6,$w6,$x6,$y6,$z6,$a7,$b7,$c7,$d 7,$e7,$f7,$g7,$h7,$i7,$j7,
      $k7,$l7,$m7,$n7,$o7,$p7,$q7,$r7,$s7,$t7,$u7,$v7,$w 7,$x7,$y7,$z7,$a8,$b8,$c8,
      $d8,$e8,$f8,$g8,$h8,$i8,$j8,$k8,$l8,$m8,$n8,$o8,$p 8,$q8,$r8,$s8,$t8,$u8,$v8,
      $w8,$x8,$y8,$z8,$a9,$b9,$c9,$d9,$e9,$f9,$g9,$h9,$i 9,$j9,$j9,$casenum,$contype) = mysql_fetch_row($result))
      [...]
      ------------
    4. Re:You should always... by WindBourne · · Score: 4, Funny

      Sadly, some will even work better.

      --
      I prefer the "u" in honour as it seems to be missing these days.
    5. Re:You should always... by Anonymous Coward · · Score: 1, Funny
      That's extreme, and at the other end of the spectrum we have PEAR.
      /*
      * (bool) isValidNum(int)
      * Returns true if the passed variable is valid 2 digit integer
      *
      * @author: ten yo
      * @param int $form_numeric_input
      * @access public
      * @return bool
      * @package PEAR wankfast
      * @class random_pear_wankfest
      */
      function isValidNum($form_numeric_input)
      {
      /* note: it's unusual for a PEAR function to be this compact
      I'm sure someone can needlessly pad it out to 50 lines */
      return (ctype_digit($form_numeric_input)<100) ? true : false;
      }
    6. Re:You should always... by Anonymous Coward · · Score: 1, Funny
      Sample comments:
      /* Now my boss wants me to update the file control routine.
      * I just updated this section last March. F***ing customers found a failing case. I hate them.
      * I'll have to cancel the beach trip this weekend, to get this done.
      * Then Fred in QA found too many Warnings. I told him to just ignore them, but no!!!!!!
      */
  3. From the "Patenting Fire" department by slipnslidemaster · · Score: 4, Funny

    I just checked the U.S. Patent office and sure enough, just minutes after your post, Microsoft patented "if (!ptr)" as a shorthand for "if (ptr==NULL)".

    Prepare to be sued.

    --


    "What the hell is an aluminum falcon?"
  4. Re:Clear Code by smittyoneeach · · Score: 3, Funny

    You mean that we should not strain after a redundant temporary object gnat and swallow a network socket camel?
    If you are caught thinking out of the box again, you will get no dessert!

    --
    Get thee glass eyes, and, like a scurvy politician, seem to see things thou dost not.--King Lear
  5. The algorithm that must not be named! by coyote-san · · Score: 4, Funny

    Grrr, you named the algorithm that must not be named! Cursed be the name of the fool who thought it would be a good algorithm for introductory students - I've lost count of the number of people convinced that this satan-spawned algorithm is faster than an insertion sort (it's not) and that there's no reason for them to learn to use the qsort() function. N.B., not to implement a quick sort, but to simply call a standard library routine.

    The most frustrating thing is that, if you must use the algorithm that must not be named, the bidirectional form of the algorithm is much faster (in practice) than the unidirectional form yet really no more complex to code than the latter if you have any potential as a software developer.

    --
    For every complex problem there is an answer that is clear, simple, and wrong. -- H L Mencken
    1. Re:The algorithm that must not be named! by GMontag451 · · Score: 3, Funny
      Hehe. Heard about bogosort? Use random permutations and check if you have the data sorted.

      The many-worlds version of bogosort is the fastest possible sorting algorithm though. Its O(C). For those that don't know, the many-worlds version just does one random permutation, with a new universe being created for each possible outcome of the permutation. You then destroy all the universes were the dataset isn't sorted.

    2. Re:The algorithm that must not be named! by TheLink · · Score: 2, Funny

      Shoot the programmer who uses bogosort.

      The programmer will be alive in the universes which bogosort worked well.

      --
  6. Whitespace causes slow programs by Anonymous Coward · · Score: 1, Funny

    Ialwaysstripallthewhitespaceoutofmyprograms.

  7. Re:Time to post the famous Knuth quote... by Anonymous Coward · · Score: 1, Funny

    We should forget about small efficiencies, about 97% of the time.

    Actually, you can get it up to 98% of the time if you are careful.

  8. Re:Write C for C programmers by Anonymous Coward · · Score: 2, Funny

    And write C++ for C++ programmers:

    try
    {
    if (1 / (int)ptr)
    ;
    cout << "ptr is not null";
    }
    catch (...)
    {
    cout << "ptr is null";
    }

  9. Optimization rules... by Anonymous Coward · · Score: 5, Funny

    When I wrote my ray-tracer for the final project of my graphics class, I used gcc -o3 and it optimized my code into Pov-ray, which was sweet. I was done with the project in like ten minutes.

    Plus I got extra credit for implementing phong shading. I didn't even try to do phong shading.

    1. Re:Optimization rules... by gardyloo · · Score: 3, Funny

      It's OK -- the great-grandparent poster optimized his humour code a little *too* much. Now we have to work on interpreters for /. readers...

  10. Re:Clear Code - Boeing by pagebt · · Score: 4, Funny

    And believe me it is a pain in the a$$. Our company did the verification for the code in the microprocessor that controls the locks to the bathroom door on a 777, if the crapper tank is full then the door locks to make sure there isn't an overflow and thus frozen turd/urine meteors that fall from the sky. Every byte of the code MUST be excercised including all error conditions.

  11. Re:Clear Code - Boeing by Anonymous Coward · · Score: 4, Funny

    So how many dumps does it take to fill up the crapper tank? I'd hate to be the last QA engineer in line to use the crapper. Also what happens when that last engineer fills up the crapper, does the bathroom door look thus trapping him inside?

  12. Re:Write C for C programmers by syukton · · Score: 2, Funny

    I've always had a problem with that whole !strcmp(x,y) thing.

    So I made a macro called STRCMP which can be used in a manner which reads better logically. ;)

    --
    Reinvent the wheel only at either a lower cost, greater effectiveness, or your own personal enrichment and satisfaction.
  13. Re:Write C for C programmers by Anonymous Coward · · Score: 1, Funny

    Fricking Genius! Name a function the exact same name as another function except make it all uppercase and make the behavior the exact opposite! If wonder if this would work for... scanf. I can #define PRINTF for scanf and that will be so much clearer! Or #define MALLOC to free. Or #define TRUE to false.

  14. Re:Clear Code - Boeing by Sponge+Bath · · Score: 5, Funny
    when that last engineer fills up the crapper, does the bathroom door look thus trapping him inside?

    HAL! Open the bathroom door!

    I'm sorry Dave, you shouldn't have had that last burrito.

  15. Never do (x == y) by zapatero · · Score: 2, Funny

    Oh you are so right. And as a corrolary never do if (x == y), because with both sides being variables one day you'll write (x = y), and (y = x) wont save you.

    The recommended way for (x == y) is
    if ( log(x) == log(y)) All the real good programmers that read magazine articles are doing this.

    And anyway, real numbers are more precise than integers. So in the case of x and y being integers its more precise using the log of the value.

    Never mind that gcc since 2.96 can issue warnings (or error with -Werror) and that languages like Java don't even allow assignments inside if(...), it's still good to practice this sound magazine article advice.

    In fact never program. Because one day you'll enter a typo, and you might have a bug, and so you should never program.

  16. Never do (x==NULL), do (!(x-NULL)) by eXocomp · · Score: 2, Funny

    "if (!(x-NULL))" is not only foolproof, but much easier to understand!

  17. Re:Clear Code - Boeing by flargleblarg · · Score: 2, Funny

    So how many dumps does it take to fill up the crapper tank?

    Man, due to budget cuts, I'd hate to be the one guy whose job that is.

    "Hang in there, you only have 17 craps to go. Here, have another burrito."