Created
May 10, 2012 02:21
-
-
Save ishworgurung/2650560 to your computer and use it in GitHub Desktop.
Comparison between std::transform vs. std::for_each (with boost::bind)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#include <vector> | |
#include <string> | |
#include <boost/bind.hpp> | |
#include <boost/ref.hpp> | |
#include <random> | |
using namespace std; | |
typedef std::vector<std::pair<std::string, int>> pair_vector; | |
class StringLengthCalculator{ | |
public: | |
void calc_length1(std::string* s){ | |
_size.push_back(std::pair<std::string,int>(*s,s->length())); | |
} | |
int calc_length2(std::string* s){ | |
return s->length(); | |
} | |
pair_vector get_result(){ | |
return _size; | |
} | |
void pp(std::pair<std::string,int> s){ | |
std::cout << s.first << " => " << s.second << std::endl; | |
} | |
private: | |
pair_vector _size; | |
int i = 0; | |
}; | |
std::random_device rd; | |
std::mt19937_64 gen(rd()); //mersenne twister | |
std::string make_string(int length, std::uniform_int_distribution<int>& d){ | |
std::string t; | |
//cout << "L:" << length << endl; | |
for(int i = 96; i < length ; ++i){ | |
//(96,122] | |
t += char(d(gen)); | |
} | |
return t; | |
} | |
int main (int argc, char* argv[]) { | |
const int ELEMENT_SIZE = atol(argv[1]); | |
cout << "element size: " << ELEMENT_SIZE << endl; | |
StringLengthCalculator s; | |
std::vector <std::string*> vec_nums; | |
double rn; | |
std::uniform_int_distribution<int> d(97, 122); | |
std::string ss; | |
for(int i = 0; i < ELEMENT_SIZE; ++i){ | |
//Create a random string of random length | |
//Fetch a random number | |
rn = d(gen); | |
//Construct a string of length "rn" | |
ss = make_string(rn, d); | |
vec_nums.push_back ( new std::string(ss.c_str()) ); | |
} | |
//cout << "vec_nums size: " << vec_nums.size() << endl; | |
if (strncmp(argv[2],"sfbb",4) == 0){ | |
std::cout << "std::for_each / boost::bind / implicit looping method\n=======================================" \ | |
<< std::endl; | |
std::for_each(vec_nums.begin(), vec_nums.end(), boost::bind(&StringLengthCalculator::calc_length1, boost::ref(s), _1)); | |
pair_vector moo = s.get_result(); | |
std::for_each(moo.begin(), moo.end(), boost::bind(&StringLengthCalculator::pp, boost::ref(s), _1)); | |
cout << "size: " << moo.size() << endl; | |
} | |
else if (strncmp(argv[2], "stbb",4) == 0){ | |
std::cout << "std::transform / boost::bind / explicit looping method\n=======================================" \ | |
<< std::endl; | |
vector<int> lengths ( vec_nums.size() ); | |
std::transform (vec_nums.begin(), vec_nums.end(), lengths.begin(), boost::bind(&StringLengthCalculator::calc_length2, boost::ref(s),_1) ); | |
for (int i=0; i<(signed int)vec_nums.size(); i++) { | |
std::cout << *vec_nums[i] << " => " << lengths[i] << endl; | |
} | |
cout << "size: " << lengths.size() << endl; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment