Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82691 - sandbox/varray/example
From: athundt_at_[hidden]
Date: 2013-02-06 20:24:16


Author: ahundt
Date: 2013-02-02 17:12:19 EST (Sat, 02 Feb 2013)
New Revision: 82691
URL: http://svn.boost.org/trac/boost/changeset/82691

Log:
added vector test with stack allocator to varray bench
Added:
   sandbox/varray/example/bench_varray.cpp (contents, props changed)
      - copied, changed from r82681, /sandbox/varray/example/bench_static_vector.cpp
   sandbox/varray/example/stack_allocator.hpp (contents, props changed)
Removed:
   sandbox/varray/example/bench_static_vector.cpp

Deleted: sandbox/varray/example/bench_static_vector.cpp
==============================================================================
--- sandbox/varray/example/bench_static_vector.cpp 2013-02-02 17:12:19 EST (Sat, 02 Feb 2013)
+++ (empty file)
@@ -1,109 +0,0 @@
-
-// benchmark based on: http://cpp-next.com/archive/2010/10/howards-stl-move-semantics-benchmark/
-/**
- * @file varray_set_example.cpp
- * @date Aug 14, 2011
- * @author Andrew Hundt <ATHundt_at_[hidden]>
- *
- * (C) 2011-2012 Andrew Hundt <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 varray_benchmark.cpp compares the performance of boost::container::varray to boost::container::vector
- *
- */
-
-#include "boost/container/varray.hpp"
-#include "boost/container/vector.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; // note: if N is too large you will run out of stack space. It is possible to increase the stack limit on some platforms.
-
-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 << "varray benchmark:\n";
- cpu_times tsv = time_it<boost::container::varray<boost::container::varray<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 << "varray/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;

Copied: sandbox/varray/example/bench_varray.cpp (from r82681, /sandbox/varray/example/bench_static_vector.cpp)
==============================================================================

Added: sandbox/varray/example/stack_allocator.hpp
==============================================================================
--- (empty file)
+++ sandbox/varray/example/stack_allocator.hpp 2013-02-02 17:12:19 EST (Sat, 02 Feb 2013)
@@ -0,0 +1,90 @@
+/*
+ An allocator which first allocates from the stack, before falling
+ back on usual std::allocator behavior. Used by signals2 to
+ optimize the vector of tracked shared_ptr created during signal
+ invocation.
+
+ Example usage:
+
+ static const std::size_t n = 10;
+ stack_storage<T, n> storage;
+ stack_allocator<T, n> a(&storage);
+ std::vector<T, stack_allocator<T, n> > v(a);
+
+*/
+// Copyright Frank Mori Hess 2008.
+// 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)
+
+// See http://www.boost.org/libs/signals2 for library home page.
+
+#ifndef BOOST_SIGNALS2_STACK_ALLOCATOR_HPP
+#define BOOST_SIGNALS2_STACK_ALLOCATOR_HPP
+
+#include <memory>
+#include <boost/noncopyable.hpp>
+#include <boost/type_traits/aligned_storage.hpp>
+#include <boost/type_traits/alignment_of.hpp>
+
+namespace boost
+{
+ namespace signals2
+ {
+ namespace detail
+ {
+ template<typename T, std::size_t n_stack_elements>
+ class stack_storage: public boost::noncopyable
+ {
+ public:
+ typedef typename boost::aligned_storage<sizeof(T), boost::alignment_of<T>::value>::type storage_type;
+ stack_storage(): is_reserved(false)
+ {
+ }
+ storage_type array[n_stack_elements];
+ bool is_reserved;
+ };
+ template<typename T, std::size_t n_stack_elements>
+ class stack_allocator: public std::allocator<T>
+ {
+ typedef std::allocator<T> base_class;
+ public:
+ template<typename U>
+ struct rebind
+ {
+ typedef stack_allocator<U, n_stack_elements> other;
+ };
+ stack_allocator(stack_storage<T, n_stack_elements> *storage = 0):
+ _storage(storage)
+ {
+ }
+ typename base_class::pointer allocate(typename base_class::size_type n_elements,
+ std::allocator<void>::const_pointer hint = 0)
+ {
+ if(_storage && _storage->is_reserved == false &&
+ n_elements <= n_stack_elements)
+ {
+ _storage->is_reserved = true;
+ return reinterpret_cast<typename base_class::pointer>(&_storage->array[0]);
+ }
+ return base_class::allocate(n_elements, hint);
+ }
+ void deallocate(typename base_class::pointer p, typename base_class::size_type n)
+ {
+ if(_storage &&
+ p == reinterpret_cast<typename base_class::pointer>(&_storage->array[0]))
+ {
+ _storage->is_reserved = false;
+ }else
+ {
+ base_class::deallocate(p, n);
+ }
+ }
+ private:
+ stack_storage<T, n_stack_elements> *_storage;
+ };
+ } // namespace detail
+ } // namespace signals2
+} // namespace boost
+
+#endif // BOOST_SIGNALS2_STACK_ALLOCATOR_HPP
\ 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