Re: [eigen] copy constructor / assignment operator potential issue? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] copy constructor / assignment operator potential issue?
- From: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>
- Date: Wed, 4 Jan 2012 14:10:24 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=2Yha0fF7dW5RigYYjimRSIyDWMEyGUIilWdojbtIk7A=; b=rsCDLOSAk4+TON1qbHqMgVz4a+b9sY57YlA3KjcUp7vYBH2Q8P1NgsdKbFx1dxq1hW RWrfI+muUn9LCocZr8H2LMYajdp+Iwc2HJpsscG8WEdNLCWWz2RVCra/ILnbUSBMLdpf 1CgifDRCJ7CEiCAQ63CQ7wwsCnfmeYxOUk8RI=
Hi,
I think that your loops are too naive, the compiler could be able to
simply remove it and only does the last copy. It is usually preferable
to wrap trivial code into a non inlined function, e.g.:
template<typename T1, typename T2>
EIGEN_DONT_INLINE void foo(T1& vec, const T2& mat, int j)
{
vec = mat.row(j);
}
and then do, e.g.:
int nr = mat.rows ();
BENCH(t1, tries, rep, for (int j = 0; j < nr; ++j) foo(vec1,mat,j););
std::cerr << t1.best() << "s\n";
BENCH(t2, tries, rep, for (int j = 0; j < mat.rows(); ++j) foo(vec2,mat,j););
std::cerr << t2.best() << "s\n";
Doing so I obtained:
Gaels-MacBook-Pro:bugs gaelguennebaud$ CXX=g++-4.2 sh run.sh
-e
a) row major + EIGEN_NO_DEBUG
0.118974s
0.118443s
-e
c) column major + EIGEN_NO_DEBUG
0.135277s
0.135402s
Gaels-MacBook-Pro:bugs gaelguennebaud$ CXX=g++-mp-4.4 sh run.sh
-e
a) row major + EIGEN_NO_DEBUG
0.081015s
0.081603s
-e
c) column major + EIGEN_NO_DEBUG
0.100763s
0.100464s
Gaels-MacBook-Pro:bugs gaelguennebaud$ CXX=g++-mp-4.6 sh run.sh
-e
a) row major + EIGEN_NO_DEBUG
0.0641789s
0.0730638s
-e
c) column major + EIGEN_NO_DEBUG
0.0778357s
0.0827175s
The results of course depend a bit on the compiler, but I don't
anything problematic here.
cheers,
gael
On Wed, Jan 4, 2012 at 10:41 AM, Radu B. Rusu <rusu@xxxxxxxxxxxxxxxx> wrote:
> int
> main (int argc, char** argv)
> {
> Vector4f vec1;
> MatrixXf mat;// (300, 4);
> mat = MatrixXf (300, 4);
> {
> int nr = mat.rows ();
> ScopeTime t1 ("w/out rows");
> for (long i = 0; i < 1000000; ++i)
> {
> for (int j = 0; j < nr; ++j)
> vec1 = mat.row (j);
> }
> }
> std::cout << vec1;
>
> Vector4f vec2;
> {
> ScopeTime t2 ("w/ rows");
> for (long i = 0; i < 1000000; ++i)
> {
> for (int j = 0; j < mat.rows (); ++j)
> vec2 = mat.row (j);
> }
> }
>
> std::cout << vec2;
> return (0);
> }