-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.cpp
More file actions
39 lines (33 loc) · 1.16 KB
/
callback.cpp
File metadata and controls
39 lines (33 loc) · 1.16 KB
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
// GPLv2 license - V. Reverdy - November 2015
// Executes n times a loop calling a callback on each element of an array
// g++ -std=c++11 -Wall -Wextra -pedantic -O3 callback.cpp -o callback_cpp
// ./callback_cpp 8192 1048576
// Preprocessor
#include <cstdlib>
#include <utility>
#include <functional>
// Benchmark function
int benchmark(int count, int size, const std::function<int(int, int)>& callback)
{
// Declares variables
int* array = (int*)(malloc(size*sizeof(int)));
int counter = 0, index = 0, sum = 0;
// Fills the array with numbers
for (index = 0; index < size; ++index) array[index] = index;
// Calls the callbacks on each element of the array and loops over it
for (counter = 0; counter < count; ++counter) {
for (index = 1; index < size; ++index) {
array[index] = callback(array[index - 1], array[index]);
}
}
// Computes the sum of elements and returns it
for (index = 0; index < size; ++index) sum += array[index];
free(array);
return sum;
}
// Main program
int main(int argc, char** argv)
{
auto function = [](int n, int m){return n + m;};
return argc * benchmark(atoi(argv[1]), atoi(argv[2]), function);
}