Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #91225

Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++
Subject Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen
Date 2023-08-09 13:52 +0200
Organization A noiseless patient Spider
Message-ID <uavumo$3ttih$1@dont-email.me> (permalink)
References <uaubnm$3i781$1@dont-email.me> <aaa09f64-8320-4b9f-bd86-91d800de81e1n@googlegroups.com> <uavbi9$3q20f$1@dont-email.me> <28833c3b-c58f-48f5-8ce4-6bbdce6f58d3n@googlegroups.com>

Show all headers | View raw


Am 09.08.2023 um 12:08 schrieb Öö Tiib:

> Great example, intersting how std::bitset<500000> and
> std::vector<bool>(500000) perform compared to that "my_set".

With this code bitset and vector<bool> have the same performance
under Windows (MSVC) and Linux (g++ / clang++).

#include <iostream>
#include <bitset>
#include <vector>
#include <random>
#include <chrono>
#include <atomic>

using namespace std;
using namespace chrono;

atomic<size_t> aSum;

int main()
{
	constexpr size_t BITS = 0x10000;
	bitset<BITS> bs;
	vector<bool> vb( BITS );
	mt19937_64 mt;
	uniform_int_distribution<size_t> iBit( 0, BITS - 1 );
	auto fill = []( auto &container )
	{
		bool bit = false;
		for( size_t i = 0; i != BITS; ++i )
			container[i] = bit,
			bit = !bit;
	};
	fill( bs );
	fill( vb );
	constexpr size_t ROUNDS = 1'000;
	auto linearBench = []( char const *what, auto const &container )
	{
		auto start = high_resolution_clock::now();
		size_t sum = 0;
		for( size_t r = ROUNDS; r--; )
			for( size_t i = 0; i != BITS; ++i )
				sum += container[i];
		double ns = duration_cast<nanoseconds>( high_resolution_clock::now() - 
start ).count() / ((double)BITS * ROUNDS);
		::aSum = sum;
		cout << what << ns << endl;
	};
	linearBench( "linear bitset: ", bs );
	linearBench( "linear vector: ", vb );
	auto gappedBench = []( char const *what, auto const &container )
	{
		auto start = high_resolution_clock::now();
		size_t sum = 0;
		for( size_t r = ROUNDS; r--; )
			for( size_t g = 0, i = 0; g != BITS; ++g, i += 7, i = i < BITS ? i : 0 )
				sum += container[i];
		double ns = duration_cast<nanoseconds>( high_resolution_clock::now() - 
start ).count() / ((double)BITS * ROUNDS);
		::aSum = sum;
		cout << what << ns << endl;
	};
	gappedBench( "gapped bitset: ", bs );
	gappedBench( "gapped vector: ", vb );
}

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

"Inside STL: The map, set, multimap, and multiset" by Raymond Chen Lynn McGuire <lynnmcguire5@gmail.com> - 2023-08-08 16:23 -0500
  Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen wij <wyniijj5@gmail.com> - 2023-08-08 17:53 -0700
    Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Paavo Helde <eesnimi@osa.pri.ee> - 2023-08-09 09:26 +0300
      Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Öö Tiib <ootiib@hot.ee> - 2023-08-09 03:08 -0700
        Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-09 13:22 +0200
        Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-09 13:52 +0200
          Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Öö Tiib <ootiib@hot.ee> - 2023-08-09 05:10 -0700
            Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-09 14:52 +0200
          Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen wij <wyniijj5@gmail.com> - 2023-08-09 07:32 -0700
            Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-09 16:36 +0200
      Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen wij <wyniijj5@gmail.com> - 2023-08-09 07:20 -0700
        Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Paavo Helde <eesnimi@osa.pri.ee> - 2023-08-09 20:29 +0300
          Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Öö Tiib <ootiib@hot.ee> - 2023-08-09 21:09 -0700
  Re: "Inside STL: The map, set, multimap, and multiset" by Raymond Chen Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-15 08:37 -0700

csiph-web