-
Notifications
You must be signed in to change notification settings - Fork 2
/
Script.java
54 lines (50 loc) · 1.48 KB
/
Script.java
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
import java.util.Arrays;
public class Script {
public static double f(int n, int m) {
double[] x = new double[n];
double y = 0.0;
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
x[j] = Math.random();
}
Arrays.sort(x);
for (j = 0; j < n; j++) {
y += x[j] * j;
}
}
return y / m / n / n;
}
public static double g(int n, int m) {
double[] x = new double[n];
double y = 0.0;
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
x[j] = Math.random();
}
for (j = 0; j < n; j++) {
y += x[j] * j;
}
}
return y / m / n / n;
}
public static void main(String[] args) {
System.out.println("Java " + Runtime.version());
int N = 15;
for (int i = 1; i <= N; i++) {
int n = (int)Math.pow(3, i);
int m = (int)Math.pow(3, N-i);
long tf0 = System.nanoTime();
double vf = f(n, m);
long tf1 = System.nanoTime();
long tg0 = System.nanoTime();
double vg = g(n, m);
long tg1 = System.nanoTime();
long tf = tf1 - tf0;
long tg = tg1 - tg0;
long Δ = tf - tg;
System.out.printf("%d %5.1f (%2.0f%%) %g\n", i, Δ/Math.pow(3,N), 100.0*Δ/tf, vf-vg);
}
}
}