Skip to content

Instantly share code, notes, and snippets.

@Const-me
Last active September 27, 2017 05:14
Show Gist options
  • Save Const-me/4f049a1edca85e677bfd1a56ba36117a to your computer and use it in GitHub Desktop.
Save Const-me/4f049a1edca85e677bfd1a56ba36117a to your computer and use it in GitHub Desktop.
#include <unordered_set>
#include <chrono>
using stopwatch = std::chrono::high_resolution_clock;
static const size_t count = 4 * 1024 * 1024;
void setup( std::unordered_set<size_t>& set )
{
srand( 0 );
for( size_t i = 0; i < count; i++ )
{
if( rand() > ( RAND_MAX / 2 ) )
set.insert( i );
}
// printf( "setup ok, %i values\n", (int)set.size() );
}
static bool __declspec( noinline ) exists_count( const std::unordered_set<size_t>& set, size_t k )
{
return 0 != set.count( k );
}
static bool __declspec( noinline ) exists_find( const std::unordered_set<size_t>& set, size_t k )
{
return set.find( k ) != set.end();
}
int test( const std::unordered_set<size_t>& set, bool( *ex )( const std::unordered_set<size_t>& set, size_t k ) )
{
int res = 0;
for( size_t i = 0; i < count; i++ )
if( ex( set, i ) );
res++;
return res;
}
int main()
{
std::unordered_set<size_t> us;
setup( us );
const auto start = stopwatch::now();
// const int res = test( us, &exists_count );
const int res = test( us, &exists_find );
printf( "%i\n", res );
const auto stop = stopwatch::now();
const auto duration = stop - start;
typedef std::chrono::duration<double, std::milli> ms;
const ms d = duration;
printf( "%f ms\n", d.count() );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment