<tb@panthema.net>
<http://www.gnu.org/licenses/>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <sys/time.h>
const size_t test_volume = 32*1024*1024;
const size_t size_min = 1024;
const size_t size_max = 1024*1024*1024;
const size_t iterations = 15;
typedef unsigned int item_type;
void test_std_sort(item_type* array, size_t n)
{
std::sort(array, array + n);
}
void test_std_stable_sort(item_type* array, size_t n)
{
std::stable_sort(array, array + n);
}
void test_std_heap_sort(item_type* array, size_t n)
{
std::make_heap(array, array + n);
std::sort_heap(array, array + n);
}
double timestamp()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1e6;
}
template <void (*test)(item_type* array, size_t n)>
void run_test(const std::string& algoname)
{
for (size_t size = size_min; size <= size_max; size *= 2)
{
size_t repeats = test_volume / size;
if (repeats == 0) repeats = 1;
std::cout << "Running algorithm " << algoname
<< " with size=" << size << " repeats=" << repeats << "\n";
for (size_t iter = 0; iter < iterations; ++iter)
{
std::cout << "iteration=" << iter << "\n";
item_type* array = new item_type[size];
for (size_t i = 0; i < size; ++i)
array[i] = i;
std::random_shuffle(array, array + size);
item_type* arraycopy = new item_type[size];
double ts1 = timestamp();
for (size_t r = 0; r < repeats; ++r)
{
std::copy(array, array + size, arraycopy);
test(array, size);
}
double ts2 = timestamp();
std::cout << "time = " << ts2-ts1 << std::endl;
delete [] arraycopy;
delete [] array;
std::cout << "RESULT"
<< " algo=" << algoname
<< " size=" << size
<< " size_log2=" << log(size) / log(2)
<< " time=" << ts2-ts1
<< " repeats=" << repeats
<< " iteration=" << iter
<< " typesize=" << sizeof(item_type)
<< " datasize=" << size * sizeof(item_type)
<< std::endl;
}
}
}
int main()
{
run_test<test_std_sort>("std::sort");
run_test<test_std_stable_sort>("std::stable_sort");
run_test<test_std_heap_sort>("std::heap_sort");
return 0;
}