第二种方法用了循环展开,速度快了不少
fun cale0(n:Long) :Unit{
var fxx = 1L
val start = System.currentTimeMillis();
System.out.println("cale0 开始 ----- ")
for (i in 1..n ) {
fxx *= i
}
val end = System.currentTimeMillis();
val d = end - start;
System.out.println("cale0 时长 ----- ${d}ms")
System.out.println("cale0 结束 ------------ $fxx")
}
/**
* n默认是4的倍数
*/
fun cale1(n:Long) :Unit{
var f0 = 1L
var f1 = 1L
var f2 = 1L
var f3 = 1L
val start = System.currentTimeMillis();
System.out.println("cale1 开始 -----")
for (i in 1..n step 4) {
f0 *= i
f1 *= i + 1
f2 *= i + 2
f3 *= i + 3
}
val end = System.currentTimeMillis();
val d = end - start;
System.out.println("cale1 时长 ----- ${d}ms")
val f = f0 * f1 * f2 * f3
System.out.println("cale1 结束 ------------ $f")
}
网友评论