Click here to Skip to main content
13,900,817 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

851 views
Posted 14 Mar 2019
Licenced CPOL

Parallel STL

, 14 Mar 2019
Rate this:
Please Sign up or sign in to vote.
Parallel STL

C++17 standard introduced execution policies to the standard algorithms; those allow for parallel and SIMD optimizations. I wanted to see how much faster the Parallel STL can be on my quad core system but none of my compilers currently support it. Luckily, Intel has implemented it and made it available to the world. πŸ™‚

On a side note, in this post’s example, I will be using several frameworks: TBB needed to compile the Parallel STL. And Catch2 to create the test benchmark. All are freely available on GitHub. BTW, thanks to Benjamin from Thoughts on CPP for pointing me toward the Catch2 library. It’s great for creating unit tests and benchmarks.

Let’s benchmark the following operations using STL and PSTL: generating random numbers, sorting the generated random numbers, finally verifying if they’re sorted. The performance increase on my quad core 2012 MacBook Pro with i7 2.3GHz is about 5x! Nice!

Program output:

Benchmark name      Iters    Elapsed ns    Average 
———————————————————–———————————————————–————————————
STL                 1        10623612832   10.6236 s 
PSTL                1        1967239761    1.96724 s 
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <vector>
#include <random>
#include <algorithm>
#include <pstl/execution>
#include <pstl/algorithm>
using namespace std;
using namespace pstl;

const unsigned long long COUNT = 100'000'000;

TEST_CASE("STL vs PSTL", "[benchmark]")
{
	auto seed = random_device{}();

	vector<int> data(COUNT);

	BENCHMARK("STL")
	{
		generate(data.begin(), data.end(), mt19937{seed});
		sort(data.begin(), data.end());
		is_sorted(data.begin(), data.end());
	}

	BENCHMARK("PSTL")
	{
		generate(pstl::execution::par_unseq, data.begin(), data.end(), mt19937{seed});
		sort(pstl::execution::par_unseq, data.begin(), data.end());
		is_sorted(pstl::execution::par_unseq, data.begin(), data.end());
	}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Martin Vorbrodt
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 15 Mar 2019
Article Copyright 2019 by Martin Vorbrodt
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid