Created
May 27, 2020 20:58
-
-
Save Jokeren/50e33a538379808ebce0fdfd27c16fb1 to your computer and use it in GitHub Desktop.
Test
This file contains 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 <chrono> | |
void __attribute__ ((noinline)) init(int *arr, size_t length) { | |
for (auto i = 0; i < length; ++i) { | |
arr[i] = 1; | |
} | |
} | |
void __attribute__ ((noinline)) compute(int *vec1, int *vec2, int *vec3, size_t length) { | |
for (auto i = 0; i < length; ++i) { | |
vec3[i] += vec1[i] * vec2[i]; | |
} | |
} | |
int main() { | |
size_t length = 1000000; | |
size_t times = 100; | |
int *vec1 = new int[length]; | |
int *vec2 = new int[length]; | |
int *vec3 = new int[length]; | |
init(vec1, length); | |
init(vec2, length); | |
init(vec3, length); | |
auto start = std::chrono::system_clock::now(); | |
for (size_t i = 0; i < times; ++i) { | |
compute(vec1, vec2, vec3, length); | |
} | |
auto end = std::chrono::system_clock::now(); | |
std::chrono::duration<double> diff = end - start; | |
std::cout << "Time: " << diff.count() << std::endl; | |
delete [] vec1; | |
delete [] vec2; | |
delete [] vec3; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment