File: vector_buf.cpp

package info (click to toggle)
libstxxl 1.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,256 kB
  • ctags: 6,830
  • sloc: cpp: 39,594; ansic: 4,217; perl: 566; sh: 555; xml: 174; makefile: 21
file content (140 lines) | stat: -rw-r--r-- 3,562 bytes parent folder | download | duplicates (5)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***************************************************************************
 *  examples/containers/vector_buf.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2013 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 <stxxl/vector>
#include <stxxl/bits/config.h>

using stxxl::uint64;

void test_vector_element(uint64 size)
{
    stxxl::scoped_print_timer tm("vector element access", 2 * size * sizeof(uint64));

//! [element]
    typedef stxxl::VECTOR_GENERATOR<uint64>::result vector_type;

    vector_type vec(size);

    for (uint64 i = 0; i < vec.size(); ++i)
        vec[i] = (i % 1024);

    uint64 sum = 0;
    for (uint64 i = 0; i < vec.size(); ++i)
        sum += vec[i];
//! [element]

    std::cout << "sum: " << sum << std::endl;
    STXXL_CHECK(sum == size / 1024 * (1024 * 1023 / 2));
}

void test_vector_iterator(uint64 size)
{
    stxxl::scoped_print_timer tm("vector iterator access", 2 * size * sizeof(uint64));

//! [iterator]
    typedef stxxl::VECTOR_GENERATOR<uint64>::result vector_type;

    vector_type vec(size);

    uint64 i = 0;
    for (vector_type::iterator it = vec.begin(); it != vec.end(); ++it, ++i)
        *it = (i % 1024);

    uint64 sum = 0;
    for (vector_type::const_iterator it = vec.begin(); it != vec.end(); ++it)
        sum += *it;
//! [iterator]

    std::cout << "sum: " << sum << std::endl;
    STXXL_CHECK(sum == size / 1024 * (1024 * 1023 / 2));
}

void test_vector_buffered(uint64 size)
{
    stxxl::scoped_print_timer tm("vector buffered access", 2 * size * sizeof(uint64));

//! [buffered]
    typedef stxxl::VECTOR_GENERATOR<uint64>::result vector_type;

    vector_type vec(size);

    // write using vector_bufwriter
    vector_type::bufwriter_type writer(vec);

    for (uint64 i = 0; i < vec.size(); ++i)
        writer << (i % 1024);

    // required to flush out the last block (or destruct the bufwriter)
    writer.finish();

    // now read using vector_bufreader
    uint64 sum = 0;

    for (vector_type::bufreader_type reader(vec); !reader.empty(); ++reader)
    {
        sum += *reader;
    }
//! [buffered]

    std::cout << "sum: " << sum << std::endl;
    STXXL_CHECK(sum == size / 1024 * (1024 * 1023 / 2));
}

#if STXXL_HAVE_CXX11_RANGE_FOR_LOOP
void test_vector_cxx11(uint64 size)
{
    stxxl::scoped_print_timer tm("vector C++11 loop access", 2 * size * sizeof(uint64));

    typedef stxxl::VECTOR_GENERATOR<uint64>::result vector_type;

    vector_type vec(size);

    {
        vector_type::bufwriter_type writer(vec);

        for (uint64 i = 0; i < vec.size(); ++i)
            writer << (i % 1024);
    }

//! [cxx11]
    // now using vector_bufreader adaptor to C++11 for loop
    uint64 sum = 0;

    for (auto it : vector_type::bufreader_type(vec))
    {
        sum += it;
    }
//! [cxx11]

    std::cout << "sum: " << sum << std::endl;
    STXXL_CHECK(sum == size / 1024 * (1024 * 1023 / 2));
}
#endif

int main(int argc, char* argv[])
{
    int multi = (argc >= 2 ? atoi(argv[1]) : 64);
    const uint64 size = multi * 1024 * uint64(1024) / sizeof(uint64);

    stxxl::block_manager::get_instance();

    test_vector_element(size);
    test_vector_iterator(size);
    test_vector_buffered(size);

#if STXXL_HAVE_CXX11_RANGE_FOR_LOOP
    test_vector_cxx11(size);
#endif

    return 0;
}