Skip to content

Commit 45a2fb5

Browse files
author
Chris Sullivan
committed
Added lambda test to example.cc.. It's faster than a c-function pointer?
1 parent 02cc083 commit 45a2fb5

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

example.cc

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ void scale(int i, double* a, double* b) {
1212
a[i]=4*b[i];
1313
}
1414

15+
struct A {
16+
17+
void scale(int i, double* a, double* b) {
18+
a[i]=4*b[i];
19+
}
20+
21+
int x = 1;
22+
23+
};
1524
int main () {
1625

1726
ThreadPool pool(8);
@@ -27,17 +36,28 @@ int main () {
2736

2837
int ntrials = 10;
2938
double tperformance = 0.0;
39+
cout << "Callable: c-function pointer\n";
3040
for (int i=0; i<ntrials; i++)
3141
{
32-
Timer timer([&](int elapsed){
33-
cout << "Trial " << i << ": "<< elapsed*1e-6 << " ms\n";
34-
tperformance+=elapsed;
35-
});
36-
pool.ParallelFor(0,N,scale,a,b);
42+
Timer timer([&](int elapsed){
43+
cout << "Trial " << i << ": "<< elapsed*1e-6 << " ms\n";
44+
tperformance+=elapsed;
45+
});
46+
pool.ParallelFor(0,N,scale,a,b);
3747
}
3848
cout << "Average: " << tperformance*1e-6 / ntrials << " ms\n\n";
3949

40-
50+
tperformance = 0.0;
51+
cout << "Callable: lambda function (without capture) \n";
52+
for (int i=0; i<ntrials; i++)
53+
{
54+
Timer timer([&](int elapsed){
55+
cout << "Trial " << i << ": "<< elapsed*1e-6 << " ms\n";
56+
tperformance+=elapsed;
57+
});
58+
pool.ParallelFor(0,N,[](int k, double* a, double* b) {return a[k] = 4*b[k];},a,b);
59+
}
60+
cout << "Average: " << tperformance*1e-6 / ntrials << " ms\n\n";
4161

4262
return 0;
4363
}

0 commit comments

Comments
 (0)