Huh, ideone shows it slower. In g++ 4.9.3 in my VM (ubuntu werewolf) I get A: 830 B: 460. In either case why is one faster than the other? I assumed A is being push by reference and B is by value but I don't see why it would do that. I tried using operator<(A a, ...
and that didn't help.
struct A {
u32 first, second;
A(int f, int s):first(f),second(s) {}
bool operator<(u32 b) const { return first < b; }
};
//bool operator<(const A&a, u32 b) { return a.first < b; }
struct B {
u64 v;
B(int f, int s) { v = ((long)f << 32) | s; }
bool operator<(u32 b) const {
return (v >> 32) < b;
}
};
u32 get_value(deque<A>& d, u32 v) {
auto p = lower_bound(d.begin(), d.end(), v);
if (p != d.end())
return p->second;
else
return UINT_MAX;
}
u32 get_value(deque<B>& d, u32 v) {
auto p = lower_bound(d.begin(), d.end(), v);
if (p != d.end())
return p->v & 0xFFFFFFFF;
else
return UINT_MAX;
}
int main(int argc, char *argv[]) {
{
deque<A> d;
struct timeval s, e;
gettimeofday(&s, 0);
for (int i = 0; i < 1024LL * 1024 * 1024 * 3 / 32; ++i)
d.emplace_back(A(i, i ^ 92142));
long v = 0;
for (int i = 0; i < 10000; ++i)
v += get_value(d, i * 3 + 1);
gettimeofday(&e, 0);
auto sec = e.tv_sec - s.tv_sec;
auto usc = e.tv_usec - s.tv_usec;
printf("A %ld\n", v);
printf("Time: %lu\n", sec * 1000 + usc / 1000);
}
{
deque<B> d;
struct timeval s, e;
gettimeofday(&s, 0);
for (int i = 0; i < 1024LL * 1024 * 1024 * 3 / 32; ++i)
d.emplace_back(B(i, i ^ 92142));
long v = 0;
for (int i = 0; i < 10000; ++i)
v += get_value(d, i * 3 + 1);
gettimeofday(&e, 0);
auto sec = e.tv_sec - s.tv_sec;
auto usc = e.tv_usec - s.tv_usec;
printf("A %ld\n", v);
printf("Time: %lu\n", sec * 1000 + usc / 1000);
}
}