3 ms·
Try to define the find() function as inline or static (but in this case the optimizer is actually too aggressive for this example and resolves the entire functi
by flohofwoe 4y ago
Try to define the find() function as inline or static (but in this case the optimizer is actually too aggressive for this example and resolves the entire function call into a constant).
- adrianN 4y agoOkay, but as far as I know "inline" is just a suggestion for the compiler. Depending on the complexity of the function or the phase of the moon the compiler can choose not to inline it. I wouldn't want to rely on that for a critical piece of my performance.
- flohofwoe 4y agoTrue, that's what I mean with depending too much on the optimizer (in a sibling comment). But IME the threshold for inlining code is surprisingly big (which of course also has a downside: potential code bloat). I added a little fix which prevents the compiler from discarding the result completely (just by going through __builtin_printf()): https://godbolt.org/z/W93WrhbaE https://godbolt.org/z/W93WrhbaE PS: another minor tweak (make the comparison function static, so that no extra copy is included in the compiler output): https://godbolt.org/z/5TTM4x193 https://godbolt.org/z/5TTM4x193
- evmar 4y ago`static` also allows it to optimize, and it's more of a code hygiene thing. Your original code as written unintentionally defines `find` as a function that must be preserved (as a separate function) into the compiled output, and `static` undoes that.