Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Last active November 27, 2020 15:53
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 Trass3r/d7c41f7b5bc113fd5b174b57c0a0b246 to your computer and use it in GitHub Desktop.
Save Trass3r/d7c41f7b5bc113fd5b174b57c0a0b246 to your computer and use it in GitHub Desktop.
comparing implementations of 'is x in a set of numbers' - https://godbolt.org/z/9en4qv
int baseline(int x)
{
switch (x)
{
case 0:
case 2:
case 8:
case 15:
case 16:
return 5;
}
return x * x;
}
#include <array>
#include <algorithm>
int usinglist(int x)
{
const std::array<int, 5> s = {
0, 2, 8, 15, 16
};
if (std::any_of(s.begin(), s.end(),
[x](int n){ return n == x; }))
return 5;
return x * x;
}
#include <set>
int usingset(int x)
{
const std::set<int> s = {
0, 2, 8, 15, 16
};
if (s.count(x))
return 5;
return x * x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment