X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ceph%2Fsrc%2Fboost%2Flibs%2Fsort%2Fexample%2Fmostlysorted.cpp;h=34e6e87ed814ef7e76c92cd0b1628fe2efd54cd6;hb=3a9019d971a194e002b14eebff7e21327a178f29;hp=b609cbdba38b37bf4613bec31141e793f387908c;hpb=5291e62e2c8c6a6d67a76e452142e9f71427378e;p=ceph.git diff --git a/ceph/src/boost/libs/sort/example/mostlysorted.cpp b/ceph/src/boost/libs/sort/example/mostlysorted.cpp index b609cbdba..34e6e87ed 100644 --- a/ceph/src/boost/libs/sort/example/mostlysorted.cpp +++ b/ceph/src/boost/libs/sort/example/mostlysorted.cpp @@ -1,100 +1,100 @@ -// spreadsort on a mostly sorted array example. -// -// Copyright Steven Ross 2009-2014. -// -// 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) - -// See http://www.boost.org/libs/sort for library home page. - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace boost::sort::spreadsort; - -#define DATA_TYPE int - -unsigned -get_index(unsigned count) -{ - unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16)); - if (result >= count || result < 0) - result = count - 1; - return result; -} - -//Pass in an argument to test std::sort -int main(int argc, const char ** argv) { - srand(1); - size_t uCount,uSize=sizeof(DATA_TYPE); - bool stdSort = false; - unsigned loopCount = 1; - for (int u = 1; u < argc; ++u) { - if (std::string(argv[u]) == "-std") - stdSort = true; - else - loopCount = atoi(argv[u]); - } - //Sorts the data once, then times sorting of already-sorted data - loopCount += 1; - std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); - if (input.fail()) { - printf("input.txt could not be opened\n"); - return 1; - } - double total = 0.0; - std::vector array; - input.seekg (0, std::ios_base::end); - size_t length = input.tellg(); - uCount = length/uSize; - input.seekg (0, std::ios_base::beg); - //Conversion to a vector - array.resize(uCount); - unsigned v = 0; - while (input.good() && v < uCount) // EOF or failure stops the reading - input.read(reinterpret_cast(&(array[v++])), uSize ); - //Run multiple loops, if requested - for (unsigned u = 0; u < loopCount; ++u) { - for (unsigned v = 0; v < uCount/10; ++v) - std::swap(array[get_index(uCount)], array[get_index(uCount)]); - clock_t start, end; - double elapsed; - start = clock(); - if (stdSort) - //std::sort(&(array[0]), &(array[0]) + uCount); - std::sort(array.begin(), array.end()); - else - //integer_sort(&(array[0]), &(array[0]) + uCount); - integer_sort(array.begin(), array.end()); - end = clock(); - elapsed = static_cast(end - start) ; - std::ofstream ofile; - if (stdSort) - ofile.open("standard_sort_out.txt", std::ios_base::out | - std::ios_base::binary | std::ios_base::trunc); - else - ofile.open("boost_sort_out.txt", std::ios_base::out | - std::ios_base::binary | std::ios_base::trunc); - if (ofile.good()) { - for (unsigned v = 0; v < array.size(); ++v) { - ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v]) ); - } - ofile.close(); - } - if (u) - total += elapsed; - } - if (stdSort) - printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); - else - printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); - return 0; -} +// spreadsort on a mostly sorted array example. +// +// Copyright Steven Ross 2009-2014. +// +// 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) + +// See http://www.boost.org/libs/sort for library home page. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace boost::sort::spreadsort; + +#define DATA_TYPE int + +unsigned +get_index(unsigned count) +{ + unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16)); + if (result >= count || result < 0) + result = count - 1; + return result; +} + +//Pass in an argument to test std::sort +int main(int argc, const char ** argv) { + srand(1); + size_t uCount,uSize=sizeof(DATA_TYPE); + bool stdSort = false; + unsigned loopCount = 1; + for (int u = 1; u < argc; ++u) { + if (std::string(argv[u]) == "-std") + stdSort = true; + else + loopCount = atoi(argv[u]); + } + //Sorts the data once, then times sorting of already-sorted data + loopCount += 1; + std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); + if (input.fail()) { + printf("input.txt could not be opened\n"); + return 1; + } + double total = 0.0; + std::vector array; + input.seekg (0, std::ios_base::end); + size_t length = input.tellg(); + uCount = length/uSize; + input.seekg (0, std::ios_base::beg); + //Conversion to a vector + array.resize(uCount); + unsigned v = 0; + while (input.good() && v < uCount) // EOF or failure stops the reading + input.read(reinterpret_cast(&(array[v++])), uSize ); + //Run multiple loops, if requested + for (unsigned u = 0; u < loopCount; ++u) { + for (unsigned v = 0; v < uCount/10; ++v) + std::swap(array[get_index(uCount)], array[get_index(uCount)]); + clock_t start, end; + double elapsed; + start = clock(); + if (stdSort) + //std::sort(&(array[0]), &(array[0]) + uCount); + std::sort(array.begin(), array.end()); + else + //integer_sort(&(array[0]), &(array[0]) + uCount); + integer_sort(array.begin(), array.end()); + end = clock(); + elapsed = static_cast(end - start) ; + std::ofstream ofile; + if (stdSort) + ofile.open("standard_sort_out.txt", std::ios_base::out | + std::ios_base::binary | std::ios_base::trunc); + else + ofile.open("boost_sort_out.txt", std::ios_base::out | + std::ios_base::binary | std::ios_base::trunc); + if (ofile.good()) { + for (unsigned v = 0; v < array.size(); ++v) { + ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v]) ); + } + ofile.close(); + } + if (u) + total += elapsed; + } + if (stdSort) + printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); + else + printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); + return 0; +}