1
Fork 0
turbonss/cxxmph/bm_urls.cc

71 lines
1.5 KiB
C++
Raw Normal View History

2011-02-14 00:40:26 +02:00
#include <fstream>
2011-02-19 00:15:24 +02:00
#include <iostream>
#include <set>
#include <string>
2011-02-14 00:40:26 +02:00
#include <vector>
2011-02-19 00:15:24 +02:00
#include <unordered_map>
2011-02-14 00:40:26 +02:00
2011-02-19 00:15:24 +02:00
#include "benchmark.h"
2011-05-16 02:47:42 +03:00
#include "mph_map.h"
2011-02-14 00:40:26 +02:00
using std::ifstream;
2011-02-19 00:15:24 +02:00
using std::set;
2011-02-14 00:40:26 +02:00
using std::string;
using std::vector;
2011-02-19 00:15:24 +02:00
namespace cxxmph {
2011-02-14 00:40:26 +02:00
2011-02-19 00:15:24 +02:00
class BM_UrlsCreate : public Benchmark {
public:
BM_UrlsCreate(int iters = 1) : Benchmark(iters) {
ReadUrls();
}
protected:
virtual void Run(int iters) {
BuildTable();
}
void BuildTable() {
for (auto it = urls_.begin(); it != urls_.end(); ++it) {
table_[*it] = it - urls_.begin();
}
table_.pack();
}
void ReadUrls() {
vector<string> urls;
std::ifstream f("URLS100k");
string buffer;
while(std::getline(f, buffer)) urls.push_back(buffer);
set<string> unique(urls.begin(), urls.end());
if (unique.size() != urls.size()) {
cerr << "Input file has repeated keys." << endl;
exit(-1);
}
urls_.swap(urls);
}
vector<string> urls_;
2011-05-16 02:47:42 +03:00
cxxmph::mph_map<string, int> table_;
2011-02-19 00:15:24 +02:00
};
class BM_UrlsFind : public BM_UrlsCreate {
public:
BM_UrlsFind(int iters = 1) : BM_UrlsCreate(iters) { ReadUrls(); BuildTable(); }
protected:
virtual void Run(int iters) {
for (int i = 0; i < iters * 100; ++i) {
int pos = random() % urls_.size();;
int h = table_[urls_[pos]];
assert(h == pos);
}
}
};
} // namespace cxxmph
using namespace cxxmph;
int main(int argc, char** argv) {
Benchmark::Register(new BM_UrlsCreate());
Benchmark::Register(new BM_UrlsFind(1000 * 1000));
Benchmark::RunAll();
2011-02-14 00:40:26 +02:00
}