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

    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.

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