On Monday, 2 April 2012 at 05:03:48 UTC, Jonathan M Davis wrote:
On Sunday, April 01, 2012 21:23:50 Jonathan M Davis wrote:
On Monday, April 02, 2012 05:46:24 L-MAN wrote:
Sure, if you have large structs, making a lot of copies of
them can be
expensive. But to avoid that, you're going to have to avoid
coding in a way
which creates temporaries, and expressions like
(abc1+abc2*20.0)+(abc1*abc2+abc1*20.0)
will _always_ create temporaries. The classic, arithmetic
operators are
classic examples of functions which create temporaries (all of
which are
rvalues). So, you can't code that way and expect to avoid
temporaries.
I should point out that with optimizations turned on, in some
cases the
compiler can optimize out the copying of temporaries, though
not generally the
temporaries themselves. For instance
auto a = abc1 + abc2 * 20.0
results in a temporary for the result of * and a temporary for
the result of
+. But the temporary for the result of + will be optimized out
(probably even
without turning on optimizations), because you're using that
result to
initialize a variable. And while the result of * will result in
a temporary,
the copy that gets made when it's passed to opBinary!"+" should
be optimized
out when compiled with optimizations turned on. So, the
optimizer should be
able to reduce the problem a fair bit if you compile with -O.
-inline may be
able to reduce it even further. So, with optimizations turned
on, you may not
get anywhere near as many copies as you expected.
And using auto ref for the parameters (which is easy with the
overloaded
arithmetic operators, since they're already templated) should
also help reduce
the number of copies made, but it _is_ up to the compiler
whether it makes a
copy or not with auto ref, so that depends on the compiler.
So, you should be able to reduce the number of copies simply by
compiling with
-O and -inline, but the temporaries themselves will exist
regardless.
- Jonathan M Davis
Thank you)) it's very good for me))