<http://www.gnu.org/licenses/>
#include <string>
#include <stdlib.h>
#include <sys/time.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stx/btree_multiset.h>
#include <assert.h>
const unsigned int insertnum = 1024000 * 32;
const int randseed = 34234235;
const int min_nodeslots = 564;
const int max_nodeslots = 564;
inline double timestamp()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 0.000001;
}
BUG
template <typename KeyType, int _bs_slots>
struct btree_traits_speed : public stx::btree_default_set_traits<KeyType>
{
static const int binsearch_threshold = _bs_slots;
};
template <int Slots>
struct Test_BtreeSet_Insert
{
typedef stx::btree_multiset<unsigned int, std::less<unsigned int>,
btree_traits_speed<unsigned int, Slots> > btree_type;
Test_BtreeSet_Insert(unsigned int) {}
void run(unsigned int insertnum)
{
btree_type bt;
srand(randseed);
for(unsigned int i = 0; i < insertnum; i++)
bt.insert( rand() );
assert( bt.size() == insertnum );
}
};
template <int Slots>
struct Test_BtreeSet_InsertFindDelete
{
typedef stx::btree_multiset<unsigned int, std::less<unsigned int>,
struct btree_traits_speed<unsigned int, Slots> > btree_type;
Test_BtreeSet_InsertFindDelete(unsigned int) {}
void run(unsigned int insertnum)
{
btree_type bt;
srand(randseed);
for(unsigned int i = 0; i < insertnum; i++)
bt.insert(rand());
assert( bt.size() == insertnum );
srand(randseed);
for(unsigned int i = 0; i < insertnum; i++)
bt.exists(rand());
srand(randseed);
for(unsigned int i = 0; i < insertnum; i++)
bt.erase_one(rand());
assert(bt.empty());
}
};
template <int Slots>
struct Test_BtreeSet_Find
{
typedef stx::btree_multiset<unsigned int, std::less<unsigned int>,
struct btree_traits_speed<unsigned int, Slots> > btree_type;
btree_type bt;
Test_BtreeSet_Find(unsigned int insertnum)
{
srand(randseed);
for(unsigned int i = 0; i < insertnum; i++)
bt.insert(rand());
assert( bt.size() == insertnum );
}
void run(unsigned int insertnum)
{
srand(randseed);
for(unsigned int i = 0; i < insertnum; i++)
bt.exists(rand());
}
};
unsigned int repeatuntil;
template <typename TestClass>
void testrunner_loop(std::ostream& os, unsigned int insertnum, unsigned int slots)
{
unsigned int runs = 0;
double ts1, ts2;
do
{
runs = 0;
{
TestClass test(insertnum);
ts1 = timestamp();
for(unsigned int totaltests = 0; totaltests <= repeatuntil; totaltests += insertnum)
{
test.run(insertnum);
++runs;
}
ts2 = timestamp();
}
std::cerr << "insertnum=" << insertnum << " slots=" << slots << " repeat=" << (repeatuntil / insertnum) << " time=" << (ts2 - ts1) << "\n";
if ((ts2 - ts1) < 1.0) repeatuntil *= 2;
}
while ((ts2 - ts1) < 1.0);
os << std::fixed << std::setprecision(10) << insertnum << " " << slots << " " << ((ts2 - ts1) / runs) << std::endl;
}
template< template<int Slots> class functional, int Low, int High>
struct test_range
{
inline void operator()(std::ostream& os, unsigned int insertnum)
{
testrunner_loop< functional<Low> >(os, insertnum, Low);
test_range<functional, Low+8, High>()(os, insertnum);
}
};
template< template<int Slots> class functional, int Low>
struct test_range<functional, Low, Low>
{
inline void operator()(std::ostream& os, unsigned int insertnum)
{
testrunner_loop< functional<Low> >(os, insertnum, Low);
}
};
int main()
{
{
std::ofstream os("tune-set-find.txt");
std::cerr << "set: find " << insertnum << "\n";
repeatuntil = insertnum;
test_range<Test_BtreeSet_Find, min_nodeslots, max_nodeslots>()(os, insertnum);
}
return 0;
}