Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
Programming Technology

Optimizations - Programmer vs. Compiler? 1422

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."
This discussion has been archived. No new comments can be posted.

Optimizations - Programmer vs. Compiler?

Comments Filter:
  • by inertia187 ( 156602 ) * on Friday February 25, 2005 @04:48PM (#11781199) Homepage Journal
    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?
  • by Anonymous Coward on Friday February 25, 2005 @04:49PM (#11781218)
    Optimize. Using cryptic, short variable names also shaves valuable microseconds off compile time and run time.
  • by slipnslidemaster ( 516759 ) on Friday February 25, 2005 @04:53PM (#11781270)
    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.
  • by FyRE666 ( 263011 ) * on Friday February 25, 2005 @04:58PM (#11781366) Homepage
    ... 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...
  • by MillionthMonkey ( 240664 ) on Friday February 25, 2005 @05:02PM (#11781444)
    But the code compiles so much faster when you turn it all into comments.
  • by holle2 ( 85109 ) on Friday February 25, 2005 @05:04PM (#11781480)
    Found on The Daily WTF http://thedailywtf.com/ShowPost.aspx?PostID=30233 [thedailywtf.com]
    ------------
    [...]
    $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))
    [...]
    ------------
  • by smittyoneeach ( 243267 ) * on Friday February 25, 2005 @05:18PM (#11781693) Homepage Journal
    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!
  • by coyote-san ( 38515 ) on Friday February 25, 2005 @05:19PM (#11781706)
    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.
  • by WindBourne ( 631190 ) on Friday February 25, 2005 @05:20PM (#11781714) Journal
    Sadly, some will even work better.
  • by Anonymous Coward on Friday February 25, 2005 @05:21PM (#11781731)
    Ialwaysstripallthewhitespaceoutofmyprograms.
  • by Anonymous Coward on Friday February 25, 2005 @05:21PM (#11781735)

    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.

  • by Anonymous Coward on Friday February 25, 2005 @05:24PM (#11781777)
    And write C++ for C++ programmers:

    try
    {
    if (1 / (int)ptr)
    ;
    cout << "ptr is not null";
    }
    catch (...)
    {
    cout << "ptr is null";
    }
  • by Anonymous Coward on Friday February 25, 2005 @05:31PM (#11781896)
    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;
    }
  • by llamalicious ( 448215 ) on Friday February 25, 2005 @05:36PM (#11781969) Journal
    And then the compiler shared his view of the universe with the programmer, who promptly committed suicide.
  • by Anonymous Coward on Friday February 25, 2005 @05:46PM (#11782107)
    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.
  • by pagebt ( 517090 ) on Friday February 25, 2005 @05:58PM (#11782277) Homepage
    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.
  • by Anonymous Coward on Friday February 25, 2005 @06:05PM (#11782362)
    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?
  • by syukton ( 256348 ) * on Friday February 25, 2005 @06:08PM (#11782394)
    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. ;)
  • by Anonymous Coward on Friday February 25, 2005 @06:23PM (#11782557)
    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!!!!!!
    */
  • by Anonymous Coward on Friday February 25, 2005 @06:25PM (#11782579)
    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.
  • by GMontag451 ( 230904 ) on Friday February 25, 2005 @06:52PM (#11782822) Homepage
    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.

  • by Sponge Bath ( 413667 ) on Friday February 25, 2005 @06:58PM (#11782895)
    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.

  • by saden1 ( 581102 ) on Friday February 25, 2005 @07:18PM (#11783077)
    HG2G has got to be the best book on the planet. Here is my favorite dialog in the book [livejournal.com].
  • by gardyloo ( 512791 ) on Friday February 25, 2005 @07:36PM (#11783236)
    It's OK -- the great-grandparent poster optimized his humour code a little *too* much. Now we have to work on interpreters for /. readers...
  • by zapatero ( 68511 ) on Friday February 25, 2005 @07:48PM (#11783340) Journal
    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.

  • by eXocomp ( 862232 ) on Friday February 25, 2005 @10:46PM (#11784580)
    "if (!(x-NULL))" is not only foolproof, but much easier to understand!
  • by flargleblarg ( 685368 ) on Friday February 25, 2005 @10:49PM (#11784601)

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

  • by edf825 ( 813288 ) on Saturday February 26, 2005 @05:31AM (#11786035)
    http://www.in.com.au/missing.html [in.com.au] ...I just had to post that :)
  • by TheLink ( 130905 ) on Saturday February 26, 2005 @07:41AM (#11786221) Journal
    Shoot the programmer who uses bogosort.

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

To the systems programmer, users and applications serve only to provide a test load.

Working...