|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r82365 - sandbox/static_vector/example
From: athundt_at_[hidden]
Date: 2013-01-05 16:21:40
Author: ahundt
Date: 2013-01-05 16:21:40 EST (Sat, 05 Jan 2013)
New Revision: 82365
URL: http://svn.boost.org/trac/boost/changeset/82365
Log:
Added simple static_vector examples and a small benchmark.
Signed-off-by: Andrew Hundt <ATHundt_at_[hidden]>
Added:
sandbox/static_vector/example/bench_static_vector.cpp (contents, props changed)
sandbox/static_vector/example/static_vector_example.cpp (contents, props changed)
sandbox/static_vector/example/static_vector_set_example.cpp (contents, props changed)
Added: sandbox/static_vector/example/bench_static_vector.cpp
==============================================================================
--- (empty file)
+++ sandbox/static_vector/example/bench_static_vector.cpp 2013-01-05 16:21:40 EST (Sat, 05 Jan 2013)
@@ -0,0 +1,96 @@
+
+// benchmark based on: http://cpp-next.com/archive/2010/10/howards-stl-move-semantics-benchmark/
+
+#include "boost/container/static_vector.hpp"
+#include "boost/container/vector.hpp"
+#include "boost/container/set.hpp"
+#include <vector>
+#include <iostream>
+#include <boost/timer/timer.hpp>
+#include <set>
+#include <algorithm>
+#include <exception>
+
+using boost::timer::cpu_timer;
+using boost::timer::cpu_times;
+using boost::timer::nanosecond_type;
+
+static const std::size_t N = 720;
+
+extern bool some_test;
+
+template<typename T>
+T get_set(std::size_t)
+{
+ T s;
+ for (std::size_t i = 0; i < N; ++i)
+ s.push_back(std::rand());
+
+ if (some_test)
+ return s;
+ return T();
+}
+
+template<typename T>
+T generate()
+{
+ T v;
+ for (std::size_t i = 0; i < N; ++i)
+ v.push_back(get_set<typename T::value_type>(i));
+ if (some_test)
+ return v;
+ return T();
+}
+
+template<typename T>
+cpu_times time_it()
+{
+ cpu_timer totalTime, stepTime;
+ {
+ T v = generate<T>();
+ totalTime.stop(); stepTime.stop();
+ std::cout << " construction took " << boost::timer::format(stepTime.elapsed());
+
+ totalTime.resume(); stepTime.start();
+ std::sort(v.begin(), v.end());
+ totalTime.stop(); stepTime.stop();
+ std::cout << " sort took " << boost::timer::format(stepTime.elapsed());
+
+ totalTime.resume(); stepTime.start();
+ std::rotate(v.begin(), v.begin() + v.size()/2, v.end());
+ totalTime.stop(); stepTime.stop();
+ std::cout << " rotate took " << boost::timer::format(stepTime.elapsed());
+
+ totalTime.resume(); stepTime.start();
+ }
+
+ totalTime.stop(); stepTime.stop();
+ std::cout << " destruction took " << boost::timer::format(stepTime.elapsed());
+ std::cout << " done\n" << std::endl;
+
+ std::cout << " Total time = " << boost::timer::format(totalTime.elapsed()) << "\n\n\n";
+ return totalTime.elapsed();
+}
+
+int main()
+{
+ try {
+ std::cout << "N = " << N << "\n\n";
+
+ std::cout << "static_vector benchmark:\n";
+ cpu_times tsv = time_it<boost::container::static_vector<boost::container::static_vector<std::size_t,N>,N > >();
+
+ std::cout << "vector benchmark\n";
+ cpu_times tv = time_it<boost::container::vector<boost::container::vector<std::size_t> > >();
+
+ std::cout << "static_vector/vector total time comparison:"
+ << "\n wall = " << ((double)tsv.wall/(double)tv.wall)
+ << "\n user = " << ((double)tsv.user/(double)tv.user)
+ << "\n system = " << ((double)tsv.system/(double)tv.system)
+ << "\n (user+system) = " << ((double)(tsv.system+tsv.user)/(double)(tv.system+tv.user)) << '\n';
+ }catch(std::exception e){
+ std::cout << e.what();
+ }
+}
+
+bool some_test = true;
Added: sandbox/static_vector/example/static_vector_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/static_vector/example/static_vector_example.cpp 2013-01-05 16:21:40 EST (Sat, 05 Jan 2013)
@@ -0,0 +1,56 @@
+/**
+ * @file static_vector_example.cpp
+ * @date Aug 14, 2011
+ * @author Andrew Hundt <ATHundt_at_[hidden]>
+ *
+ * (C) Andrew Hundt 2011-2012 <ATHundt_at_[hidden]>
+ *
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * @brief static_vector_example.cpp demonstrates the use of boost::container::static_vector
+ *
+ */
+
+#include <iostream>
+#include <string>
+#include <set>
+#include "boost/container/static_vector.hpp"
+
+using namespace std;
+using namespace boost;
+
+
+void print(std::size_t value){
+ cout << " " << value;
+}
+
+
+int main(int argc, char** argv){
+
+ // create static_vector with a capacity of 3
+ boost::container::static_vector<std::size_t,3> three; // size: 0 capacity: 3
+
+ three.push_back(5); // size: 1 capacity: 3
+ three.push_back(2); // size: 2 capacity: 3
+ three.push_back(1); // size: 3 capacity: 3
+
+ cout << "Values: ";
+ std::for_each(three.begin(),three.end(),print);
+ cout << "size: " << three.size() << " capacity: " << three.capacity() << std::endl; // size: 3 capacity: 3
+
+ // three.push_back(3); // uncomment for undefined behavior, adding a 4th element goes over the end. capacity: 3 size: 3
+
+ std::sort(three.begin(), three.end());
+ cout << "Sorted:";
+ std::for_each(three.begin(),three.end(),print);
+ cout << "size: " << three.size() << " capacity: " << three.capacity() << std::endl; // size: 3 capacity: 3
+
+ three.pop_back(); // size: 2 capacity: 3
+ three.shrink_to_fit(); // has no effect, size: 2 capacity: 3
+
+ cout << "Popped: ";
+ std::for_each(three.begin(),three.end(),print);
+ cout << " size: " << three.size() << " capacity: " << three.capacity() << std::endl; // size: 2 capacity: 3
+}
\ No newline at end of file
Added: sandbox/static_vector/example/static_vector_set_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/static_vector/example/static_vector_set_example.cpp 2013-01-05 16:21:40 EST (Sat, 05 Jan 2013)
@@ -0,0 +1,69 @@
+/**
+ * @file static_vector_set_example.cpp
+ * @date Aug 14, 2011
+ * @author Andrew Hundt <ATHundt_at_[hidden]>
+ *
+ * (C) Andrew Hundt 2011 <ATHundt_at_[hidden]>
+ *
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * @brief static_vector_set_example.cpp demonstrates the use of boost::container::static_vector
+ *
+ */
+
+#include <iostream>
+#include <string>
+#include <set>
+#include "boost/container/static_vector.hpp"
+
+namespace boost
+{
+#ifdef BOOST_NO_EXCEPTIONS
+void throw_exception(std::exception const & e){}; // user defined
+#endif // BOOST_NO_EXCEPTIONS
+} // namespace boost
+
+using namespace std;
+using namespace boost;
+
+typedef boost::container::static_vector<std::set<std::size_t>,3> ThreeSetType;
+
+void print(std::size_t value){
+ cout << " " << value;
+}
+
+
+void printSet(std::set<std::size_t> value){
+ cout << " (";
+ std::for_each(value.begin(),value.end(),print);
+ cout << ")";
+}
+
+ThreeSetType makeThreeSet(){
+ ThreeSetType t;
+ std::set<std::size_t> s;
+ s.insert(3);
+ t.push_back(s);
+ s.insert(2);
+ t.push_back(s);
+ s.insert(1);
+ t.push_back(s);
+ return t;
+}
+
+int main(int argc, char** argv){
+ cout << "Creating threeSet, a boost::container::static_vector of 3 std::set objects containing (3), (3 2), and (3 2 1), respectively" << std::endl;
+ ThreeSetType threeSet = makeThreeSet();
+ cout << "threeSet Values:" << std::endl;
+ std::for_each(threeSet.begin(),threeSet.end(),printSet);
+
+ cout << std::endl;
+ cout << "Sorting threeSet:" << std::endl;
+ std::sort(threeSet.begin(), threeSet.end());
+ std::for_each(threeSet.begin(),threeSet.end(),printSet);
+
+ cout << std::endl << "Success!" << std::endl;
+
+}
\ No newline at end of file
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk