-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.c
82 lines (64 loc) · 2.28 KB
/
benchmark.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "_bilinear.h"
float random_in_range(float min, float max) {
float scale = rand() / (float) RAND_MAX;
return min + scale * (max - min);
}
void generate_image(float *image, const long n_pixels) {
for(int i = 0; i < n_pixels; i++) {
image[i] = random_in_range(0.0, 1.0);
}
}
void generate_coordinates(float *coordinates, const int n_coordinates,
const int image_width, const int image_height) {
for(int i = 0; i < n_coordinates; i++) {
coordinates[i*2+0] = random_in_range(0.0, image_width); // x
coordinates[i*2+1] = random_in_range(0.0, image_height); // y
}
}
long diff_in_nanosecond(const struct timespec *start,
const struct timespec *end) {
long dsec = (end->tv_sec - start->tv_sec);
long dnano = (end->tv_nsec - start->tv_nsec);
return dsec * (long)1e9 + dnano;
}
void gettime(struct timespec *time) {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, time);
}
int main() {
srand(time(NULL));
const int image_width = 800;
const int image_height = 600;
const int n_pixels = image_width * image_height;
float image[n_pixels];
generate_image(image, n_pixels);
const long n_attempts = 100000;
const long n_coordinates = 8000;
float coordinates[n_coordinates*2]; // flattened representation
generate_coordinates(coordinates, n_coordinates,
image_width, image_height);
float intensities_simd[n_coordinates];
float intensities_normal[n_coordinates];
struct timespec start, end;
printf("n attempts = %ld\n", n_attempts);
printf("n coordinates = %ld\n", n_coordinates);
gettime(&start);
for(int i = 0; i < n_attempts; i++) {
_interpolation_simd(
image, image_width, coordinates,
n_coordinates, intensities_simd);
}
gettime(&end);
printf("(simd) time : %ld [ns]\n", diff_in_nanosecond(&start, &end));
gettime(&start);
for(int i = 0; i < n_attempts; i++) {
_interpolation_normal(
image, image_width, coordinates,
n_coordinates, intensities_normal);
}
gettime(&end);
printf("(normal) time : %ld [ns]\n", diff_in_nanosecond(&start, &end));
return 0;
}