0

Which code is faster in runtime? Personally, I prefer create a variable (like in "Case 2") but always doubt the speed...

Case 1:

MyClass *myClass = new MyClass();
doSomething1(myClass->getLine());
doSomething2(myClass->getLine()); 
doSomething3(myClass->getLine());  

Case 2:

MyClass *myClass = new MyClass();
std::string line = myClass->getLine();
doSomething1(line);
doSomething2(line); 
doSomething3(line);

  • 3
    Write readable code first. Only optimize when the program when it doesn't meet the erformance requirements. If you find that your program is too slow, profile it to identify bottlenecks and solve those. Don't optimize based on what you think is slow. And most importantly - trust your compiler. It's really good at optimizing code for you. – Yksisarvinen Sep 28 '22 at 13:38
  • 1
    It's impossible to tell what is faster in this particular case, because we don't know anything about the types involved. – Yksisarvinen Sep 28 '22 at 13:39
  • 4
    If you hand-optimize code to improve performance, and you have not **profiled** the before performance and the after performance, then you don't actually know if you made things better or made them worse. (I prefer case 2 because I find it more legible & maintainable. I'd probably use `auto const& line = myClass->getLine();` though. Unless each call to `getLine()` returns a *new* line of stuff.) – Eljay Sep 28 '22 at 13:40
  • It depends of what `getLine` and `doSomething1` does. If it just returns an attribute (const reference or copy) or is computationally intensive. If a `const string&` is returned and the same type is taken in parameter then no copy is involved in the first case while there is a copy in the second case. if a `std::string` is returned, then the second one will perform less copies. `std::string` copies are very fast for small string but not for big one (and compilers do not optimize this in practice). Still, premature optimization is not a good idea unless it is free to apply. – Jérôme Richard Sep 28 '22 at 13:44
  • In general I agree with profiling first. Anyway don't use new (or std::make_unique) if you don't have to. Just use MyClass directly and if you need to move it around pass it on by reference (or add move support and move it). – Pepijn Kramer Sep 28 '22 at 15:43
  • @JérômeRichard thanks, now I understand. `getLine()` returns `const string&`, I had to describe the question in more detail – Mikel Grenlou Sep 29 '22 at 08:02
  • @Eljay hm, `auto const& line` is a good tip – Mikel Grenlou Sep 29 '22 at 08:05
  • @Yksisarvinen _"Don't optimize based on what you think is slow"_ - good point, I think I'm focusing on the wrong insignificant things – Mikel Grenlou Sep 29 '22 at 08:08

1 Answers1

0

Does getLine() get a new line each time, or does it always return the same line? The first code always works, while the second code depends on how getLine() works.

And for gosh sakes, learn something about performance! DON'T JUST GUESS! And don't ask other people to guess! And I take a really dim view of profilers. Here's the method I rely on.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135