Skip to content

Instantly share code, notes, and snippets.

@messa
Created June 18, 2010 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save messa/443515 to your computer and use it in GitHub Desktop.
Save messa/443515 to your computer and use it in GitHub Desktop.
#include <string>
#include <map>
void test() {
std::map<std::string, std::string> m;
m["1"] = "foo";
m["2"] = "bar";
m["3"] = "baz";
m["4"] = "asdf";
m["5"] = "zxcv";
m["6"] = "abcd";
m["7"] = "efgh";
m["8"] = "qweqwe";
m["9"] = "werwer";
m["10"] = "hfjfhfhff";
m["1"];
m["2"];
m["3"];
m["4"];
m["5"];
m["6"];
m["7"];
m["8"];
m["9"];
m["10"];
}
int main(int argc, char *argv[]) {
for (unsigned i = 0; i < 1000000; i++) {
test();
}
return 0;
}
#include <string>
#include <vector>
class SimpleMap_t {
std::vector<std::pair<std::string, std::string> > v;
public:
SimpleMap_t() {
v.reserve(10);
}
std::string& operator[](const std::string &key) {
for (unsigned i = 0; i < v.size(); i++) {
if (v[i].first == key) {
return v[i].second;
}
}
v.push_back(std::pair<std::string,std::string>(key, ""));
return v[v.size()-1].second;
}
};
void test() {
SimpleMap_t m;
m["1"] = "foo";
m["2"] = "bar";
m["3"] = "baz";
m["4"] = "asdf";
m["5"] = "zxcv";
m["6"] = "abcd";
m["7"] = "efgh";
m["8"] = "qweqwe";
m["9"] = "werwer";
m["10"] = "hfjfhfhff";
m["1"];
m["2"];
m["3"];
m["4"];
m["5"];
m["6"];
m["7"];
m["8"];
m["9"];
m["10"];
}
int main(int argc, char *argv[]) {
for (unsigned i = 0; i < 1000000; i++) {
test();
}
return 0;
}
all: a b
echo a
time ./a
time ./a
echo b
time ./b
time ./b
a: a.cc
g++ -O2 -o a a.cc
b: b.cc
g++ -O2 -o b b.cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment