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
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <cmath>
#include <cstdlib>
#include <map>
#include "eckit/container/DenseMap.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/log/Timer.h"
#include "eckit/types/FixedString.h"
#include "eckit/utils/Translator.h"
#include "eckit/testing/Test.h"
using namespace std;
using namespace eckit;
using namespace eckit::testing;
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
#define MSIZE 500
#define NSAMPLES 50000
template <typename MAP>
void benchmark_densemap_int_string(const std::string& tname) {
std::cout << "-------------------------------------------------------------" << std::endl;
std::cout << tname << std::endl;
Translator<int, std::string> itos;
MAP m;
{
Timer timer("insert");
for (int i = 0; i < MSIZE; ++i) {
m.insert(i, "foo" + itos(i));
}
}
{
Timer timer("sort");
m.sort();
}
{
Timer timer("find");
for (int i = 0; i < NSAMPLES; ++i) {
int idx = rand() % MSIZE;
typename MAP::const_iterator it = m.find(idx);
if (it != m.cend()) {
ASSERT(it->second == "foo" + itos(idx));
}
else {
std::cout << "failed: " << idx << " " << std::string("foo") << itos(idx) << std::endl;
}
}
}
}
template <typename MAP>
void benchmark_stdmap_int_string(const std::string& tname) {
std::cout << "-------------------------------------------------------------" << std::endl;
std::cout << tname << std::endl;
Translator<int, std::string> itos;
MAP m;
{
Timer timer("insert");
for (int i = 0; i < MSIZE; ++i) {
m.insert(std::make_pair(i, "foo" + itos(i)));
}
}
{
Timer timer("find");
for (int i = 0; i < NSAMPLES; ++i) {
int idx = rand() % MSIZE;
typename MAP::const_iterator it = m.find(idx);
if (it != m.end()) {
ASSERT(it->second == "foo" + itos(idx));
}
else {
std::cout << "failed: " << idx << " " << std::string("foo") << itos(idx) << std::endl;
}
}
}
}
//----------------------------------------------------------------------------------------------------------------------
CASE("benchmark_densemap") {
benchmark_densemap_int_string<DenseMap<int, std::string> >("DenseMap<int,string>");
benchmark_stdmap_int_string<std::map<int, std::string> >("std::map<int,string>");
///
benchmark_densemap_int_string<DenseMap<int, FixedString<256> > >("DenseMap<int,FixedString>");
benchmark_stdmap_int_string<std::map<int, FixedString<256> > >("std::map<int,FixedString>");
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return run_tests(argc, argv);
}
|