All pastes #219348 Raw Edit

Mine

public text v1 · immutable
#219348 ·published 2006-10-24 17:11 UTC
rendered paste body
#include "stdafx.h"
#include "tests.h"
#include "boost/timer.hpp"
#include <list>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <hash_map>
#include <math.h>

#pragma warning ( disable : 4996 )

#define ELEM_NUM 1000000
#define FIND_NUM 1000000

/*#define FILL_HASH_MAP
#define HASH_MAP_FIND
#define CLEAR_HASH_MAP*/

/*#define FILL_MAP
#define MAP_FIND
#define CLEAR_MAP*/

#define FILL_LIST
//#define CLEAR_LIST

#define FILL_VECTOR
//#define CLEAR_VECTOR

/*#define FILL_DEQUE
#define CLEAR_DEQUE

#define FILL_SET
#define CLEAR_SET*/

void test_containers()
{
    boost::timer stopwatch;

#ifdef FILL_HASH_MAP
    cout << "\nhash_map:\n";
    stopwatch.restart();
    std::hash_map<unsigned long, unsigned char> myhashmap;
    cout << "Testing push_back ...\n";
    for (long i = 0; i < ELEM_NUM; i++)
    {
        myhashmap[i] = rand()%256;
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";

#ifdef HASH_MAP_FIND
    stopwatch.restart();
    cout << "Testing random search ...\n";
    for (long j = 0; j < FIND_NUM; j++)
    {
        std::hash_map<unsigned long, unsigned char>::iterator itr = myhashmap.find(rand()%ELEM_NUM);
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif

#ifdef CLEAR_HASH_MAP
    stopwatch.restart();
    cout << "Clearing...\n";
    myhashmap.clear();
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif
#endif

#ifdef FILL_MAP
    cout << "\nmap:\n";
    stopwatch.restart();
    std::map<unsigned long, unsigned char> mymap;
    cout << "Testing push_back ...\n";
    for (long i = 0; i < ELEM_NUM; i++)
    {
        mymap[i] = rand()%256;
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";

#ifdef MAP_FIND
    stopwatch.restart();
    cout << "Testing random search ...\n";
    for (long j = 0; j < FIND_NUM; j++)
    {
        std::map<unsigned long, unsigned char>::iterator itr = mymap.find(rand()%ELEM_NUM);
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif

#ifdef CLEAR_MAP
    stopwatch.restart();
    cout << "Clearing...\n";
    mymap.clear();
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif
#endif

#ifdef FILL_LIST
    cout << "\nlist:\n";
    stopwatch.restart();
    std::list<unsigned char> mylist;
    cout << "Testing push_back ...\n";
    for (long i = 0; i < ELEM_NUM; i++)
    {
        mylist.push_back(rand()%256);
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";

#ifdef CLEAR_LIST
    stopwatch.restart();
    cout << "Clearing...\n";
    mylist.clear();
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif
#endif

#ifdef FILL_VECTOR
    cout << "\nvector:\n";
    stopwatch.restart();
    std::vector<unsigned char> myvector;
    cout << "Testing push_back ...\n";
    for (long i = 0; i < ELEM_NUM; i++)
    {
        myvector.push_back(rand()%256);
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";

#ifdef CLEAR_VECTOR
    stopwatch.restart();
    cout << "Clearing...\n";
    myvector.clear();
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif
#endif

#ifdef FILL_DEQUE
    cout << "\ndeque:\n";
    stopwatch.restart();
    std::deque<unsigned char> mydeque;
    cout << "Testing push_back ...\n";
    for (long i = 0; i < ELEM_NUM; i++)
    {
        mydeque.push_back(rand()%256);
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";

#ifdef CLEAR_DEQUE
    stopwatch.restart();
    cout << "Clearing...\n";
    mydeque.clear();
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif
#endif

#ifdef FILL_SET
    cout << "\nset:\n";
    stopwatch.restart();
    std::set<unsigned char> myset;
    cout << "Testing push_back ...\n";
    for (long i = 0; i < ELEM_NUM; i++)
    {
        myset.insert(rand()%256);
    }
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";

#ifdef CLEAR_SET
    stopwatch.restart();
    cout << "Clearing...\n";
    myset.clear();
    std::cout << "Operation took " <<  stopwatch.elapsed() << " seconds.\n";
#endif
#endif
}