129
edits
Changes
m
''If only you knew the power of the Building Blocks''
→Efficiency Comparison Parallel for and concurrent_vector
==Efficiency Comparison Parallel for and concurrent_vector==
''If only you knew the power of the Building Blocks''
<nowiki>
#include <iostream>
#include <tbb/tbb.h>
#include <tbb/concurrent_vector.h>
#include <vector>
#include <fstream>
#include <cstring>
#include <chrono>
#include <string>
using namespace std::chrono;
// define a stl and tbb vector
tbb::concurrent_vector<std::string> con_vector_string;
std::vector<std::string> s_vector_string;
tbb::concurrent_vector<int> con_vector_int;
std::vector<int> s_vector_int;
void reportTime(const char* msg, steady_clock::duration span) {
auto ms = duration_cast<milliseconds>(span);
std::cout << msg << " - took - " <<
ms.count() << " milliseconds" << std::endl;
}
int main(int argc, char** argv){
if(argc != 2) { return 1; }
int size = std::atoi(argv[1]);
steady_clock::time_point ts, te;
/*
TEST WITH STRING OBJECT
*/
ts = steady_clock::now();
// serial for loop
for(int i = 0; i < size; ++i)
s_vector_string.push_back(std::string());
te = steady_clock::now();
reportTime("Serial vector speed - STRING: ", te-ts);
ts = steady_clock::now();
// concurrent for loop
tbb::parallel_for(0, size, 1, [&](int i){
con_vector_string.push_back(std::string());
});
te = steady_clock::now();
reportTime("Concurrent vector speed - STRING: ", te-ts);
/*
TEST WITH INT DATA TYPE
*/
std::cout<< "\n\n";
ts = steady_clock::now();
// serial for loop
for(int i = 0; i < size; ++i)
s_vector_int.push_back(i);
te = steady_clock::now();
reportTime("Serial vector speed - INT: ", te-ts);
ts = steady_clock::now();
// concurrent for loop
tbb::parallel_for(0, size, 1, [&](int i){
con_vector_int.push_back(i);
});
te = steady_clock::now();
reportTime("Concurrent vector speed - INT: ", te-ts);
}
</nowiki>
[[File:Vector_speed.png]]
'''Concept:''' Fine-grained locking