Skip to content

Instantly share code, notes, and snippets.

@sandeepkumar-skb
Created November 6, 2020 23:20
Show Gist options
  • Save sandeepkumar-skb/5bf9cea0dbeb71c9087fb731d00d421f to your computer and use it in GitHub Desktop.
Save sandeepkumar-skb/5bf9cea0dbeb71c9087fb731d00d421f to your computer and use it in GitHub Desktop.
#include "omp.h"
#include <thread>
#include <iostream>
#include <vector>
#include <chrono>
void doNothing(){
int count =0;
for (int i=0; i<1000; ++i)
++count;
}
int main(){
int algorithmToRun = 1;
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point end ;
std::chrono::duration<double> span;
start = std::chrono::high_resolution_clock::now();
for(int j=1; j<100000; ++j)
{
if(algorithmToRun == 1)
{
std::vector<std::thread> threads;
for(int i=0; i<16; i++)
{
threads.emplace_back(std::thread(doNothing));
}
for(auto& t : threads) t.join();
}
else if(algorithmToRun == 2)
{
#pragma omp parallel for num_threads(16)
for(unsigned i=0; i<16; i++)
{
doNothing();
}
}
}
end = std::chrono::high_resolution_clock::now();
span = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
std::cout << "Time per Iteration: " << (span.count()*1000)<< "ms" << std::endl;
}
@sandeepkumar-skb
Copy link
Author

Compile:
g++ -fopenmp openmp_thread_pool.cpp -o out

C++ Threads:
91559.7ms
OpenMP Threads:
538.9ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment