IAP GITLAB

Skip to content
Snippets Groups Projects
benchmark_stream.cpp 5.51 KiB
/*----------------------------------------------------------------------------
 *
 *   Copyright (C) 2021 Antonio Augusto Alves Junior
 *
 *   This file is part of RandomIterator.
 *
 *   RandomIterator is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   RandomIterator is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with RandomIterator.  If not, see <http://www.gnu.org/licenses/>.
 *
 *---------------------------------------------------------------------------*/
/*
 * benchmark_stream.hpp
 *
 *  Created on: 25/02/2021
 *      Author: Antonio Augusto Alves Junior
 */

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch.hpp>

#include <cstdio>
#include <cstdint>

//hydra
#include <random_iterator/Stream.hpp>
#include <random>

//set a global seed
static const uint64_t default_seed= 0x548c9decbce65295  ;

TEST_CASE("Performance of counter based PRNGs and corresponding iterators") {


	random_iterator::squares3_64     RNG_squares3_64(default_seed);
	random_iterator::squares4_64     RNG_squares4_64(default_seed);
	random_iterator::squares3_128    RNG_squares3_128(default_seed);
	random_iterator::squares4_128    RNG_squares4_128(default_seed);
	random_iterator::threefry        RNG_threefry(default_seed);
	random_iterator::philox          RNG_philox(default_seed);
	random_iterator::ars             RNG_ars(default_seed);

	std::mt19937_64                  RNG_mt19937(default_seed );
	std::ranlux48                    RNG_ranlux48(default_seed);

	std::uniform_real_distribution<double> dist(0.0, 1.0);

	auto squares3_64_stream  = random_iterator::make_stream( dist, RNG_squares3_64  , 1 );
    auto squares4_64_stream  = random_iterator::make_stream( dist, RNG_squares4_64  , 1 );
    auto squares3_128_stream = random_iterator::make_stream( dist, RNG_squares3_128 , 1 );
    auto squares4_128_stream = random_iterator::make_stream( dist, RNG_squares4_128 , 1 );
    auto threefry_stream     = random_iterator::make_stream( dist, RNG_threefry     , 1 );
	auto philox_stream       = random_iterator::make_stream( dist, RNG_philox       , 1 );
	auto ars_stream          = random_iterator::make_stream( dist, RNG_ars          , 1 );

	auto squares3_64_it  = squares3_64_stream.begin();
	auto squares4_64_it  = squares4_64_stream.begin();
	auto squares3_128_it = squares3_128_stream.begin();
	auto squares4_128_it = squares4_128_stream.begin();
	auto threefry_it     = threefry_stream.begin();
	auto philox_it       = philox_stream.begin();
	auto ars_it          = ars_stream.begin();



	SECTION( "Benchmarks: RNG::operator()(void)" ){

	BENCHMARK("random_iterator::squares3_64") {
		return squares3_64_stream();
	};

	BENCHMARK("random_iterator::squares4_64") {
		return squares4_64_stream();
	};

	BENCHMARK("random_iterator::squares3_128") {
		return squares3_128_stream();
	};

	BENCHMARK("random_iterator::squares4_128") {
		return squares4_128_stream();
	};

	BENCHMARK("random_iterator::threefry") {
		return threefry_stream();
	};

	BENCHMARK("random_iterator::philox") {
		return philox_stream();
	};

	BENCHMARK("random_iterator::ars") {
		return ars_stream();
	};

	}


	SECTION( "Benchmarks: *(iterator::operator()++)" )
	{

	BENCHMARK("random_iterator::squares3_64") {
		return *(squares3_64_it++);
	};

	BENCHMARK("random_iterator::squares4_64") {
		return *(squares4_64_it++);
	};

	BENCHMARK("random_iterator::squares3_128") {
		return *(squares3_128_it++);
	};

	BENCHMARK("random_iterator::squares4_128") {
		return *(squares4_128_it++);
	};

	BENCHMARK("random_iterator::threefry") {
		return *(threefry_it++);
	};

	BENCHMARK("random_iterator::philox") {
		return *(philox_it++);
	};

	BENCHMARK("random_iterator::ars") {
		return *(ars_it++);
	};

	}

	SECTION( "Benchmarks: iteraror::operator[](uint64_t)" )
	{

	BENCHMARK("random_iterator::squares3_64") {
		return squares3_64_it[10];
	};

	BENCHMARK("random_iterator::squares4_64") {
		return squares4_64_it[10];
	};

	BENCHMARK("random_iterator::squares3_128") {
		return squares3_128_it[10];
	};

	BENCHMARK("random_iterator::squares4_128") {
		return squares4_128_it[10];
	};

	BENCHMARK("random_iterator::threefry") {
		return threefry_it[10];
	};

	BENCHMARK("random_iterator::philox") {
		return philox_it[10];
	};

	BENCHMARK("random_iterator::ars") {
		return ars_it[10];
	};

	}

	SECTION( "Benchmarks: Stream<D,E>::operator[10](uint64_t)" )
	{

	BENCHMARK("random_iterator::squares3_64") {
		return squares3_64_stream[10];
	};

	BENCHMARK("random_iterator::squares4_64") {
		return squares4_64_stream[10];
	};

	BENCHMARK("random_iterator::squares3_128") {
		return squares3_128_stream[10];
	};

	BENCHMARK("random_iterator::squares4_128") {
		return squares4_128_stream[10];
	};

	BENCHMARK("random_iterator::threefry") {
		return threefry_stream[10];
	};

	BENCHMARK("random_iterator::philox") {
		return philox_stream[10];
	};

	BENCHMARK("random_iterator::ars") {
		return ars_stream[10];
	};

	}

	SECTION( "Benchmarks: std" )
	{
	BENCHMARK("std::mt19937_64") {
		return dist(RNG_mt19937);
	};

	BENCHMARK("std::ranlux48") {
		return dist(RNG_ranlux48);
	};

	}
}