Skip to content

Instantly share code, notes, and snippets.

@Climax777
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Climax777/a2703eed304444c49e21 to your computer and use it in GitHub Desktop.
Save Climax777/a2703eed304444c49e21 to your computer and use it in GitHub Desktop.
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <functional>
#define LOG(x) { std::cout << #x << " = " << (x) << "\n"; }
#define TIME(t) { std::cout << ((double)(clock() - (t)) / CLOCKS_PER_SEC) << " s\n"; }
volatile unsigned long long out = 0;
volatile unsigned long long out1 = 0;
volatile unsigned long long out2 = 0;
volatile unsigned long long out3 = 0;
void accumulate(unsigned long long number)
{
for (unsigned long long i = 0; i < number; ++i)
out += number;
}
void accumulate2(unsigned long long number)
{
for (unsigned long long i = 0; i < number; ++i)
out1 += number;
}
unsigned long long i1;
unsigned long long i2;
auto lamb1 = [=](unsigned long long number) {
for (i1 = 0; i1 < number; ++i1)
out2 += number;
};
auto lamb2 = [=](unsigned long long number) {
for ( i2 = 0; i2 < number; ++i2)
out3 += number;
};
typedef void (*sumptr)(unsigned long long);
template<typename Function>
inline void execute(unsigned long long size, Function func) {
func(size);
}
int main(int argc, char** argv)
{
const size_t MAX = 100; // number of tests
const unsigned long long SIZE = strtoull(argv[1], NULL, 10); // length of the vector
clock_t clk;
std::cout << "iterator\n";
sumptr functptr;
std::function<void(unsigned long long)> func;
std::function<void(unsigned long long)> bindfunc;
if(argc > 2) {
functptr = accumulate;
func = lamb1;
bindfunc = std::bind(&accumulate, std::placeholders::_1);
} else {
functptr = accumulate2;
func = lamb2;
bindfunc = std::bind(&accumulate2, std::placeholders::_1);
}
clk = clock();
for (size_t i = 0; i < MAX; ++i)
functptr(SIZE);
TIME(clk)
std::cout << "\nlambda\n";
clk = clock();
for (size_t i = 0; i < MAX; ++i)
func(SIZE);
/*if(argc > 2) {
for (size_t i = 0; i < MAX; ++i)
execute(SIZE, lamb1);
} else {
for (size_t i = 0; i < MAX; ++i)
execute(SIZE, lamb2);
}*/
TIME(clk)
std::cout<< out << std::endl;
std::cout<< out1 << std::endl;
std::cout<< out2 << std::endl;
std::cout<< out3 << std::endl;
std::cout << "\nbind\n";
out = 0;
out1 = 0;
clk = clock();
for (size_t i = 0; i < MAX; ++i)
bindfunc(SIZE);
TIME(clk)
std::cout<< out << std::endl;
std::cout<< out1 << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment