STXXL  1.4-dev
examples/containers/vector_buf.cpp

This example code is explained in the Efficient Sequential Reading and Writing to Vectors section.

/***************************************************************************
* examples/containers/vector_buf.cpp
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2013 Timo Bingmann <[email protected]>
*
* (See accompanying file LICENSE_1_0.txt or copy at
**************************************************************************/
#include <iostream>
#include <stxxl/vector>
void test_vector_element(uint64 size)
{
stxxl::scoped_print_timer tm("vector element access", 2 * size * sizeof(uint64));
//! [element]
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]
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]
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();
uint64 sum = 0;
{
}
//! [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));
vector_type vec(size);
{
vector_type::bufwriter_type writer(vec);
for (uint64 i = 0; i < vec.size(); ++i)
writer << (i % 1024);
}
//! [cxx11]
uint64 sum = 0;
{
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);
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;
}