From 5670e255ca2e1cad8c9dadde78e96f8c75f77879 Mon Sep 17 00:00:00 2001 From: Richard Roberts Date: Sun, 6 Nov 2011 18:19:19 +0000 Subject: [PATCH] Added timing tests for virtual classes --- gtsam/base/Makefile.am | 2 +- gtsam/base/tests/timeVirtual.cpp | 11 +++ gtsam/base/tests/timeVirtual2.cpp | 135 ++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 gtsam/base/tests/timeVirtual2.cpp diff --git a/gtsam/base/Makefile.am b/gtsam/base/Makefile.am index b1a11a0fc..1414532b7 100644 --- a/gtsam/base/Makefile.am +++ b/gtsam/base/Makefile.am @@ -36,7 +36,7 @@ sources += DSFVector.cpp check_PROGRAMS += tests/testBTree tests/testDSF tests/testDSFVector # Timing tests -noinst_PROGRAMS = tests/timeMatrix tests/timeVirtual tests/timeTest +noinst_PROGRAMS = tests/timeMatrix tests/timeVirtual tests/timeVirtual2 tests/timeTest noinst_PROGRAMS += tests/timeMatrixOps #---------------------------------------------------------------------------------------------------- diff --git a/gtsam/base/tests/timeVirtual.cpp b/gtsam/base/tests/timeVirtual.cpp index ddcaa3b17..a61ce7b77 100644 --- a/gtsam/base/tests/timeVirtual.cpp +++ b/gtsam/base/tests/timeVirtual.cpp @@ -1,3 +1,14 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + /** * @file timeVirtual.cpp * @brief Time the overhead of using virtual destructors and methods diff --git a/gtsam/base/tests/timeVirtual2.cpp b/gtsam/base/tests/timeVirtual2.cpp new file mode 100644 index 000000000..56190b91e --- /dev/null +++ b/gtsam/base/tests/timeVirtual2.cpp @@ -0,0 +1,135 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file timeVirtual.cpp + * @brief Time the overhead of using virtual destructors and methods + * @author Richard Roberts + * @date Nov 6, 2011 + */ + +#include + +#include +#include + +#include + +using namespace std; +using namespace boost; + +struct DtorTestBase { + DtorTestBase() { cout << " DtorTestBase" << endl; } + virtual ~DtorTestBase() { cout << " ~DtorTestBase" << endl; } +}; + +struct DtorTestDerived : public DtorTestBase { + DtorTestDerived() { cout << " DtorTestDerived" << endl; } + virtual ~DtorTestDerived() { cout << " ~DtorTestDerived" << endl; } +}; + + +struct VirtualBase { + VirtualBase() { } + virtual void method() = 0; + virtual ~VirtualBase() { } +}; + +struct VirtualDerived : public VirtualBase { + double data; + VirtualDerived() { data = rand(); } + virtual void method() { data = rand(); } + virtual ~VirtualDerived() { } +}; + +struct NonVirtualBase { + NonVirtualBase() { } + ~NonVirtualBase() { } +}; + +struct NonVirtualDerived : public NonVirtualBase { + double data; + NonVirtualDerived() { data = rand(); } + void method() { data = rand(); } + ~NonVirtualDerived() { } +}; + + +int main(int argc, char *argv[]) { + + // Virtual destructor test + cout << "Stack objects:" << endl; + cout << "Base:" << endl; + { DtorTestBase b; } + cout << "Derived:" << endl; + { DtorTestDerived d; } + + cout << "Heap objects:" << endl; + cout << "Base:" << endl; + { DtorTestBase *b = new DtorTestBase(); delete b; } + cout << "Derived:" << endl; + { DtorTestDerived *d = new DtorTestDerived(); delete d; } + cout << "Derived with base pointer:" << endl; + { DtorTestBase *b = new DtorTestDerived(); delete b; } + + int n = 10000000; + + VirtualBase** b = new VirtualBase*[n]; + tic_(0, "Virtual"); + tic_(0, "new"); + for(int i=0; imethod(); + toc_(1, "method"); + tic_(2, "dynamic_cast"); + for(int i=0; i(b[i]); + if(d) + d->method(); + } + toc_(2, "dynamic_cast"); + tic_(3, "delete"); + for(int i=0; imethod(); + toc_(1, "method"); + tic_(2, "dynamic_cast (does nothing)"); + for(int i=0; imethod(); + toc_(2, "dynamic_cast (does nothing)"); + tic_(3, "delete"); + for(int i=0; i