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
  • Thanks for that. A quick test showed the performance is better than either slow() or fast(). However, I don't think I can use this solution because in my real code things are slightly more complex (duh), and aa is actually a pair of functions rather than plain integers. Commented Sep 23, 2013 at 7:41
  • @JohnZwinck: I updated the answer to show how the xx() function lets you redefine auto0() and auto1() to make slow() faster than fast(). Commented Sep 23, 2013 at 19:39
  • I don't quite follow...xx() still contains a pointer to int aa[2] but in my real code I need to call aa0() and aa1() for each access. int bb[2] is really an array, but I don't see how to choose between e.g. aa0() and bb[0] using your approach. Am I missing something? Commented Sep 24, 2013 at 6:31
  • @JohnZwinck: Oh sorry, I am still answering your posted question. I can't speak to your real code, since there is nothing in your question about it. Commented Sep 24, 2013 at 8:22
  • In the real code, auto0() is like { return mode ? aa0() : bb[0]; }. And similar for auto1(). This complicates the example somewhat, but GCC makes the same "mistake" in optimizing, with an extra instruction and an extra register used in slow(). Commented Sep 24, 2013 at 9:20