Skip to content

Instantly share code, notes, and snippets.

@Const-me Const-me/set-count.cpp
Last active Sep 27, 2017

Embed
What would you like to do?
#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
You can’t perform that action at this time.