Created
May 30, 2023 11:00
-
-
Save xmvlad/726961dfefe59e9fc0c77d4cd3795eed to your computer and use it in GitHub Desktop.
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 <chrono> | |
#include <stdio.h> | |
#include <stdlib.h> | |
// Type your code here, or load an example. | |
unsigned long sum_array(unsigned long* array, unsigned long size) | |
{ | |
unsigned long sum = 0; | |
for (unsigned long i = 0; i < size; ++i) | |
sum += array[i]; | |
return sum; | |
} | |
int main() | |
{ | |
const unsigned long n = 10000000; | |
unsigned long* array = new unsigned long[n]; | |
srand(42); | |
for (unsigned long i = 0; i < n; ++i) | |
array[i] = rand(); | |
auto start = std::chrono::high_resolution_clock::now(); | |
unsigned long s = sum_array(array, n); | |
auto elapsed = std::chrono::high_resolution_clock::now() - start; | |
printf("sum: %lu\n", s); | |
printf("microseconds: %lu\n", std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment