Hacker News new | past | comments | ask | show | jobs | submit login
Will It Optimize? [2010] (ridiculousfish.com)
2 points by kibwen on Nov 30, 2013 | hide | past | favorite | 3 comments



Regarding number 2 (again):

  unsigned sum(const unsigned char *s) {
    unsigned result = 0;
    size_t length = strlen(s);
    for (size_t i=0; i < length; i++) {
      result += s[i];
    }
    return result;
  }
But it could be written as:

  unsigned sum(const unsigned char *s) {
    unsigned result = 0;
    size_t length = strlen(s);
    for (size_t i=0; i < length; ++i) // prefix increment should be quicker.
      result += s[i];
    return result;
  }


Regarding number 2: Loop-invariant strlen():

I would write the loop as the optimised version first hand, especially if there were nested loops.

No. 5 caught me out, but I'm not surprised.


I got 0/6; glad I read this.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: