[go: up one dir, main page]

Menu

[b9e44f]: / local / test2.cpp  Maximize  Restore  History

Download this file

69 lines (54 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/***************************************************************************
* local/test2.cpp
*
* This is another example file included in the local/ directory of STXXL. All
* .cpp files in local/ are automatically compiled and linked with STXXL by
* CMake. You can use this method for simple prototype applications.
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2014 Timo Bingmann <tb@panthema.net>
*
* 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)
**************************************************************************/
#include <iostream>
#include <limits>
#include <stxxl/random>
#include <stxxl/sorter>
struct my_less : std::less<int64_t>
{
int64_t min_value() const { return std::numeric_limits<int64_t>::min(); }
int64_t max_value() const { return std::numeric_limits<int64_t>::max(); }
};
int main()
{
stxxl::scoped_print_timer
timer("overall work", 600 * 1024 * 1024 * (int64_t)sizeof(int64_t));
// create sorter
stxxl::sorter<int64_t, my_less> sorter(my_less(), 256 * 1024 * 1024);
// fill sorter with random integers
{
stxxl::scoped_print_timer
timer("presort+write random numbers", 600 * 1024 * 1024 * (int64_t)sizeof(int64_t));
stxxl::random_number32 random;
for (size_t i = 0; i < 600 * 1024 * 1024; ++i) {
sorter.push(random() * random());
}
}
sorter.sort();
// get data back in sorted order
{
stxxl::scoped_print_timer
timer("read+merge random numbers", 600 * 1024 * 1024 * (int64_t)sizeof(int64_t));
int64_t first = *sorter, last = first, count = 1;
++sorter;
while (!sorter.empty())
last = *sorter, ++sorter, ++count;
// output first and last items:
std::cout << count << " items sorted ranging from "
<< first << " to " << last << std::endl;
}
return 0;
}