You could rewrite func()
to take return a Data*
instead of a Data
. Certainly it's much faster to copy a Data*
than it is to copy a Data
. But before that: do you care? Does this function run once during program initialization, taking a tenth of second of wall clock time, or do you expect to call it once a frame, or what?
When you're writing a program, or optimizing an existing one, your goal should always be to make it "fast enough" (and "responsive enough", but never mind that for now). The definition of "fast enough" varies a lot: in a simulation you might be happy to crunch one terabyte of data per hour, while in a video game you need to show the player a new graphics frame every 16 milliseconds.
So: is your program "fast enough" with just stack allocation? If it is, leave it alone. If it's not, use dynamic allocation instead. In general, it's good C++ style to return pointers instead of large structs, but you shouldn't worry about that while you're still learning. Make it work, make it right, then make it fast.