1
Fork 0
turbonss/cxxmph/bm_map.cc

113 lines
3.2 KiB
C++
Raw Normal View History

#include <string>
2011-05-24 03:18:24 +03:00
#include <tr1/unordered_map>
#include "bm_common.h"
#include "mph_map.h"
using cxxmph::mph_map;
using std::string;
2011-05-24 03:18:24 +03:00
using std::tr1::unordered_map;
namespace cxxmph {
2011-06-14 09:32:02 +03:00
uint64_t myfind(const unordered_map<uint64_t, uint64_t>& mymap, const uint64_t& k) {
2011-06-14 08:24:40 +03:00
return mymap.find(k)->second;
}
2011-06-14 10:59:54 +03:00
2011-06-14 08:24:40 +03:00
uint64_t myfind(const mph_map<uint64_t, uint64_t>& mymap, const uint64_t& k) {
2011-06-14 09:32:02 +03:00
return mymap.find(k)->second;
2011-06-14 08:24:40 +03:00
}
const StringPiece& myfind(const unordered_map<StringPiece, StringPiece, Murmur2StringPiece>& mymap, const StringPiece& k) {
return mymap.find(k)->second;
}
StringPiece myfind(const mph_map<StringPiece, StringPiece>& mymap, const StringPiece& k) {
auto it = mymap.find(k);
return it->second;
}
template <class MapType>
2011-06-14 08:24:40 +03:00
class BM_CreateUrls : public UrlsBenchmark {
public:
2011-06-14 08:24:40 +03:00
BM_CreateUrls(const string& urls_file) : UrlsBenchmark(urls_file) { }
virtual void Run() {
MapType mymap;
for (auto it = urls_.begin(); it != urls_.end(); ++it) {
mymap[*it] = *it;
}
}
};
template <class MapType>
2011-06-14 08:24:40 +03:00
class BM_SearchUrls : public SearchUrlsBenchmark {
public:
2011-06-14 08:24:40 +03:00
BM_SearchUrls(const std::string& urls_file, int nsearches)
2011-05-24 03:18:24 +03:00
: SearchUrlsBenchmark(urls_file, nsearches) { }
virtual void Run() {
for (auto it = random_.begin(); it != random_.end(); ++it) {
2011-06-14 10:59:54 +03:00
auto v = myfind(mymap_, *it);
if (v != *it) {
fprintf(stderr, "Looked for %s got %s\n", it->data(), v.data());
exit(-1);
}
}
}
protected:
2011-05-24 03:18:24 +03:00
virtual bool SetUp() {
2011-06-13 08:16:19 +03:00
if (!SearchUrlsBenchmark::SetUp()) return false;
for (auto it = urls_.begin(); it != urls_.end(); ++it) {
mymap_[*it] = *it;
}
2011-05-24 03:18:24 +03:00
mymap_.rehash(mymap_.bucket_count());
return true;
}
MapType mymap_;
};
2011-06-14 08:24:40 +03:00
template <class MapType>
class BM_SearchUint64 : public SearchUint64Benchmark {
public:
2011-06-14 09:32:02 +03:00
BM_SearchUint64() : SearchUint64Benchmark(10000, 10*1000*1000) { }
2011-06-14 08:24:40 +03:00
virtual bool SetUp() {
if (!SearchUint64Benchmark::SetUp()) return false;
for (int i = 0; i < values_.size(); ++i) {
mymap_[values_[i]] = values_[i];
}
mymap_.rehash(mymap_.bucket_count());
2011-06-14 09:32:02 +03:00
// Double check if everything is all right
for (int i = 0; i < values_.size(); ++i) {
if (mymap_[values_[i]] != values_[i]) return false;
}
2011-06-14 08:24:40 +03:00
return true;
}
virtual void Run() {
for (auto it = random_.begin(); it != random_.end(); ++it) {
auto v = myfind(mymap_, *it);
2011-06-14 09:32:02 +03:00
if (v != *it) {
fprintf(stderr, "Looked for %lu got %lu\n", *it, v);
exit(-1);
}
2011-06-14 08:24:40 +03:00
}
}
MapType mymap_;
};
} // namespace cxxmph
using namespace cxxmph;
int main(int argc, char** argv) {
2011-06-14 09:32:02 +03:00
srandom(4);
2011-06-14 08:24:40 +03:00
/*
Benchmark::Register(new BM_CreateUrls<mph_map<StringPiece, StringPiece>>("URLS100k"));
Benchmark::Register(new BM_CreateUrls<unordered_map<StringPiece, StringPiece>>("URLS100k"));
*/
2011-06-14 09:32:02 +03:00
Benchmark::Register(new BM_SearchUrls<mph_map<StringPiece, StringPiece>>("URLS100k", 10*1000* 1000));
2011-06-14 10:59:54 +03:00
Benchmark::Register(new BM_SearchUrls<unordered_map<StringPiece, StringPiece, Murmur2StringPiece>>("URLS100k", 10*1000* 1000));
2011-06-14 08:24:40 +03:00
/*
2011-06-14 09:32:02 +03:00
Benchmark::Register(new BM_SearchUint64<unordered_map<uint64_t, uint64_t>>);
2011-06-14 08:24:40 +03:00
Benchmark::Register(new BM_SearchUint64<mph_map<uint64_t, uint64_t>>);
*/
Benchmark::RunAll();
}