Hello, both codes below do exactly the same and both are working. Here is my question for those more experienced. Which of the two codes below would be the most efficient when working with large vectors?
Version 1 is exactly n times faster than version 2.
This is because you're returning a vector. This means that the vector needs to copied when returned. I don't need to say what this would entail for long vectors or big elements. It could take a while.
Version 1 is more or less as efficient as it gets.
Nope. Just as fast as version 1 in the other post.
Notice, however, that you're not deleting vec after you're done with it.
This is not a problem in this case, since the program terminates immediately, but if that was a function that is called several times you would create quite a few memory leaks.
PS: I realized soon after that my calculation in the above post was wrong, but I was too lazy to change it. Version 2 takes a little less than twice as long as version 1.
Ok. As always, thanks for your valuable reply. My concern is I am working with large scale problems, so my vectors are large. With this in mind, do you know if there is a more efficient way to do the operations above? or version 1 or the pointer version are the fastest way I can work?