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/
.
分享到:
相关推荐
Modulus Full Family Font
linux下从公钥指数(Exponent)和模数(Modulus)得到RSA公钥和pem公钥文件,完整C代码,可直接编译验证。 在网上找了好久都没有合适的,自己做了一个完整的demo。
SimpleModulus for MuOnline
java代码-Modulus
公钥加密
PC2002 Modulus software
小波模极大值原理在图像边缘提取和信号奇异点检测中的应用
git clone git@gitlab.com:nvidia/modulus/modulus.git
用Verilog HDL设计一个4bit变模计数器和...在4bit输入cipher的控制下,实现同步模5、模8、模10、模12及用任务调用语句实现的5bit二进制加法器,计数器具有同步清零和暂停计数的功能。主频为50MHz,要求显示频率为1Hz。
The elastic modulus of Matrigel as determined by atomic force microscopy
模数 SSL 示例一个简单的静态服务器,提供一个页面来...跑步 $ node siteServer listening on port 8080部署到 Modulus 不需要特殊的过程。 如果可用,服务器将使用process.env.PORT 。 $ modulus deploy -p <project>
GMW 16827-2013 Polyolefin Alloy - 1700 MPa Flexural Modulus.pdf
模数演示 一个用户可以部署到 Modulus.io 的小型演示项目。 该演示包括一个小的快速示例和一些使用 Pulse 图形引擎的客户端乐趣。
局域键平均近似固体热弹性,李佳,Li Yangxian,An analytical solution has been developed from the perspective of local bond average to correlating the elastic modulus of a specimen to the length and ...
模运算符是由百分号(%)表示。代码相关,博文链接http://u.bb/355623/1425-modulus-operator
用小波的模极大值法有效检测噪声图像的边缘(Detecting the edge of noisy image by modulus maximum method of wavelet transform)
变温拉曼振频与弹性模量的相关性,Mingxia Gu,Likun Pan,An analytical solution is presented correlating the elastic modulus and the vibronic frequency in a specimen with consistent understanding of the...
modulus 13552351350449497466140191726480947600097303807021690643242001981410540990...
LED Edit software 234
对matlab典型数学模型和算法进行了描述,列举实例进行计算,并建立模型。