[mlpack-svn] [MLPACK] #118: Use consistent accessors and mutators
MLPACK Trac
trac at coffeetalk-1.cc.gatech.edu
Thu Oct 27 17:05:24 EDT 2011
#118: Use consistent accessors and mutators
------------------------------------------+---------------------------------
Reporter: rcurtin | Owner:
Type: wishlist | Status: new
Priority: major | Milestone: MLPACK 1.0
Component: MLPACK | Resolution:
Keywords: mlpack getter setter public | Blocking: 120
Blocked By: |
------------------------------------------+---------------------------------
Comment (by nslagle):
As I understand it, the ''inline'' keyword may or may not do what we
intend; the compiler still has the final say on whether it chooses to
expand functions inline. Also, it appears that defining methods inside of
the class body implicitly suggests to the compiler that said methods are
candidates for ''inline'' expansion.
Compiling the following with level zero optimization, ''getInt'' and
''setInt'' both appear in the executable.
{{{
#include <iostream>
#include <err.h>
#include <stdlib.h>
class TestInline
{
private:
int anInt;
public:
TestInline() {}
int getInt() { return anInt; }
inline void setInt(int s);
};
void TestInline::setInt(int s) { anInt = s; }
int main (int argc, char* argv[])
{
if (argc != 2)
{
errx (1, "Usage: ./test_inline <loops>");
}
size_t loop = atoi (argv[1]);
size_t sum = 0;
TestInline* tester = new TestInline();
for (size_t k = 0; k < loop; ++k)
{
sum += k;
tester->setInt(sum);
}
tester->setInt(3);
std:: cout << "anInt " << tester->getInt() << std::endl;
return 0;
}
}}}
Recompiling with optimization level one or above (''-O1''), ''getInt''
disappears from the executable. Adding ''inline'' to the prototype for
''setInt'' gives similar results.
With such simple ''gets'' and ''sets''. the execution improves
considerably.
{{{
time ./test_inline 1000000000
anInt 3
real 0m7.475s
user 0m7.448s
sys 0m0.024s
time ./test_inline 1000000000
anInt 3
real 0m0.610s
user 0m0.607s
sys 0m0.001s
}}}
The first uses no inlining (''-O0''), the second uses inlining (''-O1'').
Using a command line argument and a complicated summation should prevent
the compiler from simply removing the loop; if it were optimizing
intelligently by noting that the final set value is loop * (loop + 1) / 2,
the execution time would be more like 0m0.001s rather than 600 times
higher.
Provided the compiler actually will inline the setter and getter for
''Range'' bounds, I suggest we do so.
--
Ticket URL: <http://trac.research.cc.gatech.edu/fastlab/ticket/118#comment:10>
MLPACK <www.fast-lab.org>
MLPACK is an intuitive, fast, and scalable C++ machine learning library developed by the FASTLAB at Georgia Tech under Dr. Alex Gray.
More information about the mlpack-svn
mailing list