Ad
  • Custom User Avatar

    A reference/pointer is often 8 bytes long on the x64 architecture, so it's always pointless for primitive types unless you want to actually change them

  • Custom User Avatar
  • Custom User Avatar
  • Default User Avatar
  • Default User Avatar

    Very Code Complete solution there.

  • Custom User Avatar

    Agreed. It's not too bad since vectors are cleverly resized under the hood. But you are right, it would be an improvement. Calling the constructor with the size would also be an option. While doing that you could also store the size in a variable and use that for the loop. That should be more efficient, since the compiler probably can't infer that lines.size() is a constant

  • Default User Avatar

    Of Course we will see more "mov' assembler instructions, if we will work with references. Nevertheless the only way to measure optimization of "mov' instructions is time for the CPU to deal with them. As we can see from the benchmarking the CPU managed to sort out 7 additional mov instructions in less time. To summarise, to look only for the amount of generated instructions isn't a precise way to reach a conclusion of optimisation each time.

  • 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.

  • Default User Avatar

    Hi. I have checked my solution using the chrono library in debug mode.
    Passing by value: 577 microseconds.
    Passing by reference: 386 microseconds.
    Release mode
    Passing by value: 270 microseconds.
    Passing by reference: 271 microseconds.
    Therefore I cant agree with your statement. If you have another idea how to practically measure the difference in another way - please tell me.
    I have spotted an idea of yours in a book, however I try to check everything by myself.

  • 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 :)