Ad
  • Custom User Avatar

    Benchmarking is tricky and unreliable (depends on too many factors), especially for such small examples. A better metric is to simply check the generated assembly and look at the instruction count. You can use Compiler Explorer for that. Here's a comparison using your solution: https://godbolt.org/z/EY3EK753r. Passing by reference generates 7 additional instructions in this case.

  • Custom User Avatar

    Passing a basic type (such as int, char, bool, float etc) by reference can actually be less efficient than passing it by value. Remember that a reference is just a pointer and it's address still needs to be copied to the function (and since the address is an int, it's exactly as costly as passing by value). And then there's also the overhead of dereferencing that pointer (basically the * operator, which the language is hiding from us when we use references, but it's still there). So all in all, it's not worth it!

  • Custom User Avatar

    Passing a basic type (such as int, char, bool, float etc) by reference can actually be less efficient than passing it by value. Remember that a reference is just a pointer and it's address still needs to be copied to the function (and since the address is an int, it's exactly as costly as passing by value). And then there's also the overhead of dereferencing that pointer (basically the * operator, which the language is hiding from us when we use references, but it's still there). So all in all, it's not worth it!

  • Custom User Avatar

    Since you know the final size of the vector (lines.size()), you should really try to reserve the space beforehand (with the #reserve method). Otherwise you will get a lot of unnecessary allocations and copying with those "push_back" methods when the vector grows.

  • Custom User Avatar

    The inline here is redundant, because constexpr implies inline :)