Skip to content

Instantly share code, notes, and snippets.

@xmvlad
Created May 30, 2023 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmvlad/726961dfefe59e9fc0c77d4cd3795eed to your computer and use it in GitHub Desktop.
Save xmvlad/726961dfefe59e9fc0c77d4cd3795eed to your computer and use it in GitHub Desktop.
#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