-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.c
65 lines (62 loc) · 1.57 KB
/
script.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int compare(const void *a, const void *b) {
double *x = (double *)a;
double *y = (double *)b;
if (*x < *y)
return -1;
else if (*x > *y)
return 1;
else
return 0;
}
double f(int n, int m) {
double *x = malloc(n * sizeof(double));
double y = 0.0;
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
x[j] = rand() / (double)RAND_MAX;
}
qsort(x, n, sizeof(double), compare);
for (j = 0; j < n; j++) {
y += x[j] * j;
}
}
return y / m / n / n;
}
double g(int n, int m) {
double *x = malloc(n * sizeof(double));
double y = 0.0;
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
x[j] = rand() / (double)RAND_MAX;
}
for (j = 0; j < n; j++) {
y += x[j] * j;
}
}
return y / m / n / n;
}
int main() {
printf("C gcc %s\n", __VERSION__);
int N = 15;
for (int i = 1; i <= N; i++) {
int n = pow(3, i);
int m = pow(3, N-i);
float tf0 = (float)clock()/CLOCKS_PER_SEC;
double vf = f(n, m);
float tf1 = (float)clock()/CLOCKS_PER_SEC;
float tg0 = (float)clock()/CLOCKS_PER_SEC;
double vg = g(n, m);
float tg1 = (float)clock()/CLOCKS_PER_SEC;
float tf = tf1 - tf0;
float tg = tg1 - tg0;
float Δ = tf - tg;
printf("%d %5.1f (%2.0f%%) %g\n", i, Δ*pow(10,9)/pow(3,N), 100*Δ/tf, vf-vg);
}
return 0;
}