Created
February 21, 2023 19:26
-
-
Save xmvlad/f5b9a13fa56007d215343cb3ca2c104a to your computer and use it in GitHub Desktop.
Measure perfomance for counting numbers at array
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 <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <time.h> | |
#ifdef _MSC_VER | |
#include <intrin.h> | |
#else | |
#include <x86intrin.h> | |
#endif | |
int64_t hardware_timestamp() | |
{ | |
return __rdtsc(); | |
} | |
void count_numbers(uint32_t* num_array, uint32_t* count_array, size_t N) | |
{ | |
for (size_t i = 0; i < N; ++i) | |
count_array[num_array[i]] += 1; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if (argc != 2) | |
{ | |
printf("Need array size parameter\n"); | |
return 1; | |
} | |
size_t array_size = strtoull(argv[1], NULL, 0); | |
printf("Array size: %lu\n", array_size); | |
if (array_size == 0) | |
{ | |
printf("Incorrect array size\n"); | |
return 1; | |
} | |
srand(time(NULL)); | |
printf("Rand max: %u\n", RAND_MAX); | |
uint32_t* num_array = new uint32_t[array_size]; | |
uint32_t* count_array = new uint32_t[array_size]; | |
for (int i = 0; i < array_size; ++i) | |
{ | |
num_array[i] = rand() % array_size; | |
count_array[i] = 0; | |
} | |
uint64_t start_tick = hardware_timestamp(); | |
count_numbers(num_array, count_array, array_size); | |
uint64_t tick_duration = hardware_timestamp() - start_tick; | |
printf("Processor cycles: %llu, per array element: %f\n", tick_duration, tick_duration/(double)array_size); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment