-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-w-time.cpp
77 lines (60 loc) · 2.17 KB
/
test-w-time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Lambda
#include "lambda-commented.h"
// Required
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
// Timing
#include <time.h>
#include <sys/time.h>
// Boost Lambda
#include <boost/lambda/lambda.hpp>
using namespace std;
class Plus1 {
public:
int operator()(int &v);
};
int Plus1::operator()(int &v) {
return v + 1;
}
struct plus_1 : public unary_function<int, int> {
int operator()(int v){ return v + 1; }
};
int main(void) {
/* Timing */
struct timeval tv_initial, tv_finish;
struct timezone tz;
int i = 0;
/* Initial Vector of 1,000,000 integers */
vector<int> v;
for(i = 0; i < 1000000; i++) {
v.push_back(i);
}
//printf("=== Lambda Library \\w transform() ===\n");
gettimeofday(&tv_initial, &tz);
transform(v.begin(), v.end(), v.begin(), _1 + 1);
gettimeofday(&tv_finish, &tz);
printf("%d,", ((int)(tv_finish.tv_sec - tv_initial.tv_sec) * 1000000) + (int)(tv_finish.tv_usec - tv_initial.tv_usec));
//printf("=== Plus1 class with Functor ===\n");
gettimeofday(&tv_initial, &tz);
transform(v.begin(), v.end(), v.begin(), Mult10());
gettimeofday(&tv_finish, &tz);
printf("%d,", ((int)(tv_finish.tv_sec - tv_initial.tv_sec) * 1000000) + (int)(tv_finish.tv_usec - tv_initial.tv_usec));
//printf("=== plus_1 unary_function ===\n");
gettimeofday(&tv_initial, &tz);
transform(v.begin(), v.end(), v.begin(), plus_1());
gettimeofday(&tv_finish, &tz);
printf("%d,", ((int)(tv_finish.tv_sec - tv_initial.tv_sec) * 1000000) + (int)(tv_finish.tv_usec - tv_initial.tv_usec));
//printf("=== <functional> ===\n");
gettimeofday(&tv_initial, &tz);
transform(v.begin(), v.end(), v.begin(), bind2nd(std::plus<int>(), 1));
gettimeofday(&tv_finish, &tz);
printf("%d,", ((int)(tv_finish.tv_sec - tv_initial.tv_sec) * 1000000) + (int)(tv_finish.tv_usec - tv_initial.tv_usec));
//printf("=== boost::lambda ===\n");
gettimeofday(&tv_initial, &tz);
transform(v.begin(), v.end(), v.begin(), boost::lambda::_1 + 1);
gettimeofday(&tv_finish, &tz);
printf("%d\n", ((int)(tv_finish.tv_sec - tv_initial.tv_sec) * 1000000) + (int)(tv_finish.tv_usec - tv_initial.tv_usec));
return 0;
}