Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 3
    Wh do you think that rearrangement is for optimizing the running code instead of minimizing the source? All else being equal, shorter code is easier to comprehend because there's less of it. Commented Aug 18, 2017 at 20:24
  • 1
    @Deduplicator I can't agree with shorter code being easier to comprehend. I've edited my answer to propose a C++ solution (OP accepts any language) using function pointers, but it could be implemented in any language that supports lambda functions. The source is shorter, but the generated code will be slower, and the human mind will have to think twice to follow a logic that was very natural in OP's initial code. ;-) Commented Aug 18, 2017 at 21:09
  • 1
    Well, "all else being equal". Naturally, that condition does not always apply. Commented Aug 18, 2017 at 22:06
  • "Don't diddle code to make it faster; find a better algorithm." This quote makes no sense to me. For one thing, I'm already trying to find a better algorithm. Also, where is the line drawn between diddling code to make it faster and finding a better algorithm? There doesn't seem to be a clear distinction. Commented Aug 20, 2017 at 23:09
  • @KyleDelaney didling code is to change for find small optimisations in order to gain to gain a couple of ms here and there but without changing fundamentally the approach. Finding a better algorithm consist of using a different approach to solve the problem. I agree that your approach is somewhat btw the two, I can remove this quote if it upsets you. Commented Aug 20, 2017 at 23:24