Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C Implementation of benchmark test
#include <stdlib.h>
#include <stdio.h>
// We're using int64_t as `usize` and `Int` in Rust / Swift also have 8byte
int64_t* resize_image(int64_t* image, int64_t size, int64_t width, int64_t scale, int64_t* rsize) {
int64_t *result = malloc(sizeof(int64_t) * (size / scale));
int64_t pos = 0;
for (int64_t i=0; i<size; i+=width) {
for (int64_t i2=i; i2<(i + width); i2+=scale) {
int64_t sum = 0;
for (int64_t i3=i2; i3<(i2 + scale); i3+=1) {
sum += image[i3];
}
result[pos++] = sum / scale;
}
}
*rsize = pos / sizeof(int64_t);
return result;
}
int64_t* generate(int64_t* size) {
const int64_t count = 200000;
int64_t image[] = {
1, 0, 0, 4, 4, 0, 0, 1,
0, 0, 0, 9, 9, 0, 0, 0,
0, 0, 0, 9, 9, 0, 0, 0,
4, 9, 9, 9, 9, 9, 9, 4,
4, 9, 9, 9, 9, 9, 9, 4,
0, 0, 0, 9, 9, 0, 0, 0,
0, 0, 0, 9, 9, 0, 0, 0,
1, 0, 0, 4, 4, 0, 0, 1,
};
int64_t* array = malloc(sizeof(int64_t) * count * sizeof(image));
int64_t pos = 0;
for (int64_t i=0; i<count; i++) {
for (int64_t i=0; i<sizeof(image); i+=1) {
array[pos++] = image[i];
}
}
*size = (count * sizeof(image));
return array;
}
int main(int argc, const char * argv[]) {
int64_t image_size = 0;
int64_t* image = generate(&image_size);
int64_t result1_size, result2_size, result3_size;
int64_t *result1 = resize_image(image, image_size, 8, 2, &result1_size);
int64_t *result2 = resize_image(image, image_size, 32, 8, &result2_size);
int64_t *result3 = resize_image(image, image_size, 16, 4, &result3_size);
free(result1);
free(result2);
free(result3);
printf("%lli %lli %lli\n", result1_size, result2_size, result3_size);
free(image);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.