EvbCFfp1XB

problem and my answer.


     lg(N)  (lg(N))^2    N^(1/2)          N     Nlg(N) N(lg(N))^2    N^(5/4)    N^(3/2)        N^2  N^2*lg(N)        N^3
3.3219 11.035 3.1623 10.000 33.219 110.35 17.783 31.623 100.00 332.19 1000.0
6.6439 44.141 10.000 100.00 664.39 4414.1 316.23 1000.0 10000 66439 1.0000e+06
9.9658 99.317 31.623 1000.0 9965.8 99317 5623.4 31623 1.0000e+06 9.9658e+06 1.0000e+09
13.288 176.56 100.00 10000 1.3288e+05 1.7656e+06 1.0000e+05 1.0000e+06 1.0000e+08 1.3288e+09 1.0000e+12
16.610 275.88 316.23 1.0000e+05 1.6610e+06 2.7588e+07 1.7783e+06 3.1623e+07 1.0000e+10 1.6610e+11 1.0000e+15
19.932 397.27 1000.0 1.0000e+06 1.9932e+07 3.9727e+08 3.1623e+07 1.0000e+09 1.0000e+12 1.9932e+13 1.0000e+18
23.253 540.73 3162.3 1.0000e+07 2.3253e+08 5.4073e+09 5.6234e+08 3.1623e+10 1.0000e+14 2.3253e+15 1.0000e+21
26.575 706.25 10000 1.0000e+08 2.6575e+09 7.0625e+10 1.0000e+10 1.0000e+12 1.0000e+16 2.6575e+17 1.0000e+24
29.897 893.85 31623 1.0000e+09 2.9897e+10 8.9385e+11 1.7783e+11 3.1623e+13 1.0000e+18 2.9897e+19 1.0000e+27
33.219 1103.5 1.0000e+05 1.0000e+10 3.3219e+11 1.1035e+13 3.1623e+12 1.0000e+15 1.0000e+20 3.3219e+21 1.0000e+30

source code

public class PrintRoughEstimateValue {
public static void main(String[] args) {
String[] s = new String[] { "lg(N)", "(lg(N))^2", "N^(1/2)", "N",
"Nlg(N)", "N(lg(N))^2", "N^(5/4)", "N^(3/2)", "N^2",
"N^2*lg(N)", "N^3", };
for (int i = 0; i < s.length; i++) {
System.out.format("%10s", s[i]);
if (i < s.length - 1) {
System.out.print(" ");
} else {
System.out.println();
}
}

double[][] d = new double[10][];
for (int i = 0; i < d.length; i++) {
double n = Math.pow(10, i + 1);
double log = Math.log(n) / Math.log(2);
d[i] = new double[] { log, log * log, Math.pow(n, 1d / 2d), n,
n * log, n * (log * log), Math.pow(n, 5d / 4d),
Math.pow(n, 3d / 2d), Math.pow(n, 2d), n * n * log,
Math.pow(n, 3d), };
}
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
System.out.format("%10s", String.format("%.5g", d[i][j]));
if (j < d[i].length - 1) {
System.out.print(" ");
} else {
System.out.println();
}
}
}
}
}




second process
0.000001051 if (n > Integer.MAX_VALUE) {}
0.000001057 n = 123 - 456;
0.000001073 n = 123 ^ 456;
0.000001110 if (n > 1023) {}
0.000001117 n = 123 % 456;
0.000001125 n++;
0.000001133 n = 1;
0.000001140 n = 123 + 456;
0.000001148 n = 123 | 456;
0.000001156 n = 123 * 456;
0.000001162 n = 123 / 456;
0.000001163 if (n > 0) {}
0.000001177 n = 123 & 456;
0.000001208 n = Integer.MAX_VALUE;
0.000001335 int n = 0;
0.000001425 if ((a + b) > (c - d)) {n = a * b;} else {n = c / d;}
0.000001778 //
0.000001906 a[0] = 1;
0.000002100 int c = (a <= b) ? a : b;
0.000002910 int[] a;
0.000002918 System.nanoTime();
0.000003878 System.nanoTime(); * 2
0.000004125 a = new int[10];
0.000004133 for (int i = 0; i < 10; i++) {}
0.000004471 a = new int[100];
0.000005431 System.nanoTime(); * 3
0.000006480 System.nanoTime(); * 4
0.000007216 for (int i = 0; i < 100; i++) {}
0.000007921 System.nanoTime(); * 5
0.000010680 System.out.print("");
0.000012946 Math.min(a, b);
0.000013156 function();
0.000013921 a = new int[1000];
0.000020139 new Object();
0.000022576 hypot(a, b);
0.000029035 swap(a, 3, 5);
0.000036175 map.put(time, "new Object();");
0.000037226 System.out.format("");
0.000050862 System.arraycopy(a, 0, b, 0, a.length(10000));
0.000056937 for (int i = 0; i < 1000; i++) {}
0.000061505 a = new int[10000];
0.000070251 System.out.print(""); first time
0.000073829 System.out.println();
0.000073926 Math.hypot(a, b);
0.000129168 System.out.format("%s","");
0.000208705 TreeMap<Long, String> map = new TreeMap<Long, String>();
0.000496053 a = new int[100000];
0.000563310 for (int i = 0; i < 10000; i++) {}
0.000949315 System.out.println(); first time
0.001125272 Math.random();
0.001148643 for (int i = 0; i < a.length(10000); i++) {a[i] = i;}
0.001519286 for (int i = 0; i < b.length(10000); i++) {b[i] = a[i];}
0.003442330 System.out.format("%s",""); first time
0.006092340 for (int i = 0; i < 100000; i++) {}
0.007315006 for (int i = 0; i < 1000000; i++) {}
0.010598474 a = new int[1000000];
0.027848038 for (int i = 0; i < 10000000; i++) {}
0.096182962 System.out.format(""); first time
0.280941199 for (int i = 0; i < 100000000; i++) {}

source code
import java.util.TreeMap;

public class TimeOfProcess {
public static void main(String[] args) {
long start;
long time;

start = System.nanoTime();
TreeMap<Long, String> map = new TreeMap<Long, String>();
map.put(System.nanoTime() - start,
"TreeMap<Long, String> map = new TreeMap<Long, String>();");

nanoTime(map);

start = System.nanoTime();
function();
map.put(System.nanoTime() - start, "function();");

start = System.nanoTime();
new Object();
time = System.nanoTime() - start;

start = System.nanoTime();
map.put(time, "new Object();");
map.put(System.nanoTime() - start, "map.put(time, \"new Object();\");");

loop(map);

array(map);

math(map);

print(map);

integer(map);

System.out.format("%11s %s\n", "second", "process");
for (long key : map.keySet()) {
System.out.format("%11.9f %s\n", (key) / 1e9, map.get(key));
}
}

private static void nanoTime(TreeMap<Long, String> map) {
long start;
start = System.nanoTime();
//
map.put(System.nanoTime() - start, "//");

start = System.nanoTime();
;
map.put(System.nanoTime() - start, ";");

start = System.nanoTime();
System.nanoTime();
map.put(System.nanoTime() - start, "System.nanoTime();");

start = System.nanoTime();
System.nanoTime();
System.nanoTime();
map.put(System.nanoTime() - start, "System.nanoTime(); * 2");

start = System.nanoTime();
System.nanoTime();
System.nanoTime();
System.nanoTime();
map.put(System.nanoTime() - start, "System.nanoTime(); * 3");

start = System.nanoTime();
System.nanoTime();
System.nanoTime();
System.nanoTime();
System.nanoTime();
map.put(System.nanoTime() - start, "System.nanoTime(); * 4");

start = System.nanoTime();
System.nanoTime();
System.nanoTime();
System.nanoTime();
System.nanoTime();
System.nanoTime();
map.put(System.nanoTime() - start, "System.nanoTime(); * 5");
}

private static void loop(TreeMap<Long, String> map) {
long start;

for (int k = 1; k <= 8; k++) {
int n = (int) Math.pow(10, k);
start = System.nanoTime();
for (int i = 0; i < n; i++) {
}
map.put(System.nanoTime() - start, "for (int i = 0; i < " + n
+ "; i++) {}");
}

}

private static void print(TreeMap<Long, String> map) {
long start;
start = System.nanoTime();
System.out.println();
map.put(System.nanoTime() - start, "System.out.println(); first time");

start = System.nanoTime();
System.out.println();
map.put(System.nanoTime() - start, "System.out.println();");

start = System.nanoTime();
System.out.print("");
map.put(System.nanoTime() - start, "System.out.print(\"\"); first time");

start = System.nanoTime();
System.out.print("");
map.put(System.nanoTime() - start, "System.out.print(\"\");");

start = System.nanoTime();
System.out.format("");
map.put(System.nanoTime() - start,
"System.out.format(\"\"); first time");

start = System.nanoTime();
System.out.format("");
map.put(System.nanoTime() - start, "System.out.format(\"\");");

start = System.nanoTime();
System.out.format("%s", "");
map.put(System.nanoTime() - start,
"System.out.format(\"%s\",\"\"); first time");

start = System.nanoTime();
System.out.format("%s", "");
map.put(System.nanoTime() - start, "System.out.format(\"%s\",\"\");");
}

private static void array(TreeMap<Long, String> map) {
long start;

start = System.nanoTime();
int[] a;
map.put(System.nanoTime() - start, "int[] a;");

for (int i = 1; i <= 6; i++) {
int n = (int) Math.pow(10, i);
start = System.nanoTime();
a = new int[n];
map.put(System.nanoTime() - start, "a = new int[" + n + "];");
}

a = new int[10000];

start = System.nanoTime();
a[0] = 1;
map.put(System.nanoTime() - start, "a[0] = 1;");

start = System.nanoTime();
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
map.put(System.nanoTime() - start, "for (int i = 0; i < a.length("
+ a.length + "); i++) {a[i] = i;}");

start = System.nanoTime();
swap(a, 3, 5);
map.put(System.nanoTime() - start, "swap(a, 3, 5);");

int[] b = new int[a.length];
start = System.nanoTime();
System.arraycopy(a, 0, b, 0, a.length);
map.put(System.nanoTime() - start,
"System.arraycopy(a, 0, b, 0, a.length(" + a.length + "));");

start = System.nanoTime();
for (int i = 0; i < b.length; i++) {
b[i] = a[i];
}
map.put(System.nanoTime() - start, "for (int i = 0; i < b.length("
+ b.length + "); i++) {b[i] = a[i];}");
}

private static void math(TreeMap<Long, String> map) {
int a = 123;
int b = 456;
long start;

start = System.nanoTime();
Math.random();
map.put(System.nanoTime() - start, "Math.random();");

start = System.nanoTime();
Math.min(a, b);
map.put(System.nanoTime() - start, "Math.min(a, b);");

start = System.nanoTime();
int c = (a <= b) ? a : b;
map.put(System.nanoTime() - start, "int c = (a <= b) ? a : b;");

start = System.nanoTime();
Math.hypot(a, b);
map.put(System.nanoTime() - start, "Math.hypot(a, b);");

start = System.nanoTime();
hypot(a, b);
map.put(System.nanoTime() - start, "hypot(a, b);");
}

private static void integer(TreeMap<Long, String> map) {
long start;

start = System.nanoTime();
int n = 0;
map.put(System.nanoTime() - start, "int n = 0;");

start = System.nanoTime();
n = 1;
map.put(System.nanoTime() - start, "n = 1;");

start = System.nanoTime();
n = Integer.MAX_VALUE;
map.put(System.nanoTime() - start, "n = Integer.MAX_VALUE;");

start = System.nanoTime();
n++;
map.put(System.nanoTime() - start, "n++;");

start = System.nanoTime();
if (n > 0) {
}
map.put(System.nanoTime() - start, "if (n > 0) {}");

start = System.nanoTime();
if (n > 1023) {
}
map.put(System.nanoTime() - start, "if (n > 1023) {}");

start = System.nanoTime();
if (n > Integer.MAX_VALUE) {
}
map.put(System.nanoTime() - start, "if (n > Integer.MAX_VALUE) {}");

start = System.nanoTime();
n = 123 + 456;
map.put(System.nanoTime() - start, "n = 123 + 456;");

start = System.nanoTime();
n = 123 - 456;
map.put(System.nanoTime() - start, "n = 123 - 456;");

start = System.nanoTime();
n = 123 * 456;
map.put(System.nanoTime() - start, "n = 123 * 456;");

start = System.nanoTime();
n = 123 / 456;
map.put(System.nanoTime() - start, "n = 123 / 456;");

start = System.nanoTime();
n = 123 % 456;
map.put(System.nanoTime() - start, "n = 123 % 456;");

start = System.nanoTime();
n = 123 & 456;
map.put(System.nanoTime() - start, "n = 123 & 456;");

start = System.nanoTime();
n = 123 | 456;
map.put(System.nanoTime() - start, "n = 123 | 456;");

start = System.nanoTime();
n = 123 ^ 456;
map.put(System.nanoTime() - start, "n = 123 ^ 456;");

{
int a = (int) (100 * Math.random());
int b = (int) (100 * Math.random());
int c = (int) (100 * Math.random());
int d = (int) (100 * Math.random());
start = System.nanoTime();
if ((a + b) > (c - d)) {
n = a * b;
} else {
n = c / d;
}
map.put(System.nanoTime() - start,
"if ((a + b) > (c - d)) {n = a * b;} else {n = c / d;}");
}
}

private static void function() {
}

private static double hypot(int x, int y) {
return Math.sqrt(x * x + y * y);
}

private static void swap(int[] a, int i, int j) {
int s = a[i];
a[i] = a[j];
a[j] = s;
}
}

このページのトップヘ