Stxxl  1.4.0
containers/btree/test_const_scan.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/btree/test_const_scan.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2006 Roman Dementiev <dementiev@ira.uka.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #include <iostream>
00014 
00015 #include <stxxl/bits/containers/btree/btree.h>
00016 #include <stxxl/timer>
00017 
00018 
00019 struct comp_type : public std::less<int>
00020 {
00021     static int max_value()
00022     {
00023         return (std::numeric_limits<int>::max)();
00024     }
00025 };
00026 
00027 #define NODE_BLOCK_SIZE 4096
00028 #define LEAF_BLOCK_SIZE 128 * 1024
00029 
00030 struct my_type
00031 {
00032     double data;
00033     char filler[24];
00034 };
00035 
00036 std::ostream & operator << (std::ostream & o, const my_type & obj)
00037 {
00038     o << " " << obj.data;
00039     return o;
00040 }
00041 
00042 std::ostream & operator << (std::ostream & o, const std::pair<int, double> & obj)
00043 {
00044     o << obj.first << " " << obj.second;
00045     return o;
00046 }
00047 
00048 typedef stxxl::btree::btree<int, my_type, comp_type,
00049                             NODE_BLOCK_SIZE, LEAF_BLOCK_SIZE, stxxl::SR> btree_type;
00050 
00051 #define node_cache_size (25 * 1024 * 1024)
00052 #define leaf_cache_size (6 * LEAF_BLOCK_SIZE)
00053 
00054 void NC(btree_type & BTree)
00055 {
00056     double sum = 0;
00057     stxxl::timer Timer1;
00058     Timer1.start();
00059     btree_type::iterator it = BTree.begin();
00060     btree_type::iterator end = BTree.end();
00061     for ( ; it != end; ++it)
00062         sum += it->second.data;
00063 
00064     Timer1.stop();
00065     STXXL_MSG("Scanning with non const iterator: " << Timer1.mseconds() << " msec");
00066 }
00067 
00068 void C(btree_type & BTree)
00069 {
00070     double sum = 0;
00071     stxxl::timer Timer1;
00072     Timer1.start();
00073     btree_type::const_iterator it = BTree.begin();
00074     btree_type::const_iterator end = BTree.end();
00075     for ( ; it != end; ++it)
00076         sum += it->second.data;
00077 
00078     Timer1.stop();
00079     STXXL_MSG("Scanning with const iterator: " << Timer1.mseconds() << " msec");
00080 }
00081 
00082 int main(int argc, char * argv[])
00083 {
00084     if (argc < 2)
00085     {
00086         STXXL_MSG("Usage: " << argv[0] << " #ins");
00087         return -1;
00088     }
00089 
00090     const unsigned nins = atoi(argv[1]);
00091 
00092     STXXL_MSG("Data set size  : " << nins * sizeof(std::pair<int, my_type>) << " bytes");
00093     STXXL_MSG("Node cache size: " << node_cache_size << " bytes");
00094     STXXL_MSG("Leaf cache size: " << leaf_cache_size << " bytes");
00095 
00096     //stxxl::random_number32 rnd;
00097 
00098     std::vector<std::pair<int, my_type> > Data(nins);
00099 
00100     for (unsigned int i = 0; i < nins; ++i)
00101     {
00102         Data[i].first = i;
00103     }
00104     {
00105         btree_type BTree1(Data.begin(), Data.end(), comp_type(), node_cache_size, leaf_cache_size, true);
00106         btree_type BTree2(Data.begin(), Data.end(), comp_type(), node_cache_size, leaf_cache_size, true);
00107 
00108 
00109         //STXXL_MSG(*stxxl::stats::get_instance());
00110 
00111         C(BTree1);
00112 
00113         //STXXL_MSG(*stxxl::stats::get_instance());
00114 
00115         NC(BTree2);
00116 
00117         //STXXL_MSG(*stxxl::stats::get_instance());
00118     }
00119 
00120     {
00121         btree_type BTree1(Data.begin(), Data.end(), comp_type(), node_cache_size, leaf_cache_size, true);
00122         btree_type BTree2(Data.begin(), Data.end(), comp_type(), node_cache_size, leaf_cache_size, true);
00123 
00124         STXXL_MSG("Disabling prefetching");
00125         BTree1.disable_prefetching();
00126         BTree2.disable_prefetching();
00127 
00128         //STXXL_MSG(*stxxl::stats::get_instance());
00129 
00130         C(BTree1);
00131 
00132         //STXXL_MSG(*stxxl::stats::get_instance());
00133 
00134         NC(BTree2);
00135 
00136         //STXXL_MSG(*stxxl::stats::get_instance());
00137     }
00138     STXXL_MSG("All tests passed successfully");
00139 
00140     return 0;
00141 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines