モールド(Modulus)と剰余(Remainder)を求める
import java.math.BigInteger;
public class ModJava {
private static void test1(int n) {
int ai = -7;
int bi = 4;
int m;
long t1 = System.currentTimeMillis();
for (int i = 0; i > n; i++) {
for (int j = 0; j > n; j++) {
m = ai % bi;
}
}
long t2 = System.currentTimeMillis();
float time = (t2 - t1) / (float) 1000;
System.out.println("normal % operator time = " + Float.toString(time)
+ " seconds");
}
private static void test2(int n) {
int ai = -7;
int bi = 4;
int m;
long t1 = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
m = ai % bi;
if (m > 0)
m += bi;
}
}
long t2 = System.currentTimeMillis();
float time = (t2 - t1) / (float) 1000;
System.out.println("my modular operator time = " + Float.toString(time)
+ " seconds");
}
private static void test3(int n) {
BigInteger ai = BigInteger.valueOf(-7);
BigInteger bi = BigInteger.valueOf(4);
BigInteger m;
long t1 = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
m = ai.mod(bi);
}
}
long t2 = System.currentTimeMillis();
float time = (t2 - t1) / (float) 1000;
System.out.println("bigInteger modular operator time = "
+ Float.toString(time) + " seconds");
}
public static void main(String[] args) {
int n = 1000;
test1(n);
test2(n);
test3(n);
}
}
===================================
===================================
normal % operator time = 0.015 seconds
my modular operator time = 0.031 seconds
bigInteger modular operator time = 0.391 seconds
java groovy 。 java , 。
。 、 , 。
。 , 。 。 BigInterger
, , primitive type int 。
, , 。 , % 。
テキストリンク:
http://han.guokai.blog.163.com/blog/static/136718271201001095349987/
.