Java 为什么在netbeans中,一种方法的运行时间比另一种方法要长?
当实现两个方法遵循相同的算法时,我想计算每个方法的运行时间。但我注意到,代码较少的方法比其他方法花费的时间更多。 以下是第一种代码较少但运行时间较长的方法:Java 为什么在netbeans中,一种方法的运行时间比另一种方法要长?,java,Java,当实现两个方法遵循相同的算法时,我想计算每个方法的运行时间。但我注意到,代码较少的方法比其他方法花费的时间更多。 以下是第一种代码较少但运行时间较长的方法: public void encryptFiles(List<BloomFilter> bfList1) { StopWatch watch = new StopWatch(); watch.start("calculate time"); for (BloomFilte
public void encryptFiles(List<BloomFilter> bfList1) {
StopWatch watch = new StopWatch();
watch.start("calculate time");
for (BloomFilter bfList2 : bfList1) {
Random raa = new Random();
int g1 = raa.nextInt();
double m1 = (double) ((double) Math.round(g1 * 10) / 10.0);
List<double[]> res1 = new ArrayList<>();
double[] e1 = new double[400];
double[] k1 = new double[400];
Vector<Double> j = new Vector<Double>(400);
Vector<Double> h = new Vector<Double>(400);
String bfName= bfList2.getName();
for (int i = 0; i < s.size(); i++) {
if (s.get(i) == 1) {
j.add( (double) bfList2.getBitSet().getWord(i));
h.add((double) bfList2.getBitSet().getWord(i));
} else {
j.add(0.5 * (bfList2.getBitSet().getWord(i))+m1);
h.add(0.5 * (bfList2.getBitSet().getWord(i))+m1 );
}
}
for (int u = 0; u < 400; u++) {
for (int y = 0; y < 400; y++) {
e1[u] +=a2[u][y]*j.get(y);
k1[u] +=b2[u][y]*h.get(y);
}
}
res1.add(e1);
res1.add(k1);
hasssh.put(bfName,res1 );
}
watch.stop();
encryptedBFListInTime=watch.getTotalTimeSeconds();
System.out.println("time only for encrypt bloom filter"+encryptedBFListInTime);
//System.out.println("encrypt files only in "+encryptedBFListInTime);
}
public BloomFilterIndex encryptTree(BloomFilterIndex tree) {
StopWatch watch1=new StopWatch();
watch1.start("calculate");
for(int m=0;m<tree.root.children.size();m++){
BloomFilterIndex.BFINode<Integer> n=(BloomFilterIndex.BFINode<Integer>)tree.root.children.get(m);
encrypt(n);
}
watch1.stop();
end=watch1.getTotalTimeSeconds();
System.out.println("encrypt tree only in :"+end);
return tree;
}
public void encrypt(BloomFilterIndex.BFINode<Integer> root) {
List<double[]> ress = new ArrayList<>();
if(!root.isLeaf()){
c = new double[root.value.size()];
for (int i = 0; i < root.value.size(); i++) {
c[i] =root.value.getBitSet().getWord(i);
}
ress.add(c);
root.value = null;
root.value2 = ress;
for(BloomFilterIndex.BFINode<Integer> g:root.children)
encrypt(g);
} else{
double[] y = new double[400];
double[] z = new double[400];
Random r = new Random();
Integer g1 = r.nextInt();
double m5 = (double) ((double) Math.round(g1 * 10) / 10.0);
Vector<Double> m6 = new Vector<Double>(400);
Vector<Double> n1 = new Vector<Double>(400);
for (int i = 0; i < s.size(); i++) {
if (s.get(i) == 1) {
m6.add((double) root.value.getBitSet().getWord(i));
n1.add((double) root.value.getBitSet().getWord(i));
} else {
m6.add( 0.5 * (root.value.getBitSet().getWord(i))+m5);
n1.add( 0.5 * (root.value.getBitSet().getWord(i))+m5 );
}
}
for (int i = 0; i < 400;i++) {
for (int j = 0; j < 400; j++) {
y[i] += a2[i][j] * m6.get(j);
z[i] += b2[i][j] * n1.get(j);
}
}
ress.add(y);
ress.add(z);
root.value = null;
root.value2 = ress;
}
}
i used all function to calculate running time `System.currentTimeMills()`,`Systen.nanoTime()`, `instant.now()`, `new Date.getTime(), localTime.now()` all of thes give the same result and i try to use `JMH benchmark` but cannot running it.
public void加密文件(列表bfList1){
秒表=新秒表();
观察。开始(“计算时间”);
对于(BloomFilter bfList2:bfList1){
随机raa=新随机();
int g1=raa.nextInt();
双m1=(双)((双)数学四舍五入(g1*10)/10.0);
List res1=new ArrayList();
双精度[]e1=新双精度[400];
double[]k1=新的double[400];
向量j=新向量(400);
向量h=新向量(400);
字符串bfName=bfList2.getName();
对于(int i=0;i
另一种方法具有相同的代码和一些额外的步骤,但运行时间较短:
public void encryptFiles(List<BloomFilter> bfList1) {
StopWatch watch = new StopWatch();
watch.start("calculate time");
for (BloomFilter bfList2 : bfList1) {
Random raa = new Random();
int g1 = raa.nextInt();
double m1 = (double) ((double) Math.round(g1 * 10) / 10.0);
List<double[]> res1 = new ArrayList<>();
double[] e1 = new double[400];
double[] k1 = new double[400];
Vector<Double> j = new Vector<Double>(400);
Vector<Double> h = new Vector<Double>(400);
String bfName= bfList2.getName();
for (int i = 0; i < s.size(); i++) {
if (s.get(i) == 1) {
j.add( (double) bfList2.getBitSet().getWord(i));
h.add((double) bfList2.getBitSet().getWord(i));
} else {
j.add(0.5 * (bfList2.getBitSet().getWord(i))+m1);
h.add(0.5 * (bfList2.getBitSet().getWord(i))+m1 );
}
}
for (int u = 0; u < 400; u++) {
for (int y = 0; y < 400; y++) {
e1[u] +=a2[u][y]*j.get(y);
k1[u] +=b2[u][y]*h.get(y);
}
}
res1.add(e1);
res1.add(k1);
hasssh.put(bfName,res1 );
}
watch.stop();
encryptedBFListInTime=watch.getTotalTimeSeconds();
System.out.println("time only for encrypt bloom filter"+encryptedBFListInTime);
//System.out.println("encrypt files only in "+encryptedBFListInTime);
}
public BloomFilterIndex encryptTree(BloomFilterIndex tree) {
StopWatch watch1=new StopWatch();
watch1.start("calculate");
for(int m=0;m<tree.root.children.size();m++){
BloomFilterIndex.BFINode<Integer> n=(BloomFilterIndex.BFINode<Integer>)tree.root.children.get(m);
encrypt(n);
}
watch1.stop();
end=watch1.getTotalTimeSeconds();
System.out.println("encrypt tree only in :"+end);
return tree;
}
public void encrypt(BloomFilterIndex.BFINode<Integer> root) {
List<double[]> ress = new ArrayList<>();
if(!root.isLeaf()){
c = new double[root.value.size()];
for (int i = 0; i < root.value.size(); i++) {
c[i] =root.value.getBitSet().getWord(i);
}
ress.add(c);
root.value = null;
root.value2 = ress;
for(BloomFilterIndex.BFINode<Integer> g:root.children)
encrypt(g);
} else{
double[] y = new double[400];
double[] z = new double[400];
Random r = new Random();
Integer g1 = r.nextInt();
double m5 = (double) ((double) Math.round(g1 * 10) / 10.0);
Vector<Double> m6 = new Vector<Double>(400);
Vector<Double> n1 = new Vector<Double>(400);
for (int i = 0; i < s.size(); i++) {
if (s.get(i) == 1) {
m6.add((double) root.value.getBitSet().getWord(i));
n1.add((double) root.value.getBitSet().getWord(i));
} else {
m6.add( 0.5 * (root.value.getBitSet().getWord(i))+m5);
n1.add( 0.5 * (root.value.getBitSet().getWord(i))+m5 );
}
}
for (int i = 0; i < 400;i++) {
for (int j = 0; j < 400; j++) {
y[i] += a2[i][j] * m6.get(j);
z[i] += b2[i][j] * n1.get(j);
}
}
ress.add(y);
ress.add(z);
root.value = null;
root.value2 = ress;
}
}
i used all function to calculate running time `System.currentTimeMills()`,`Systen.nanoTime()`, `instant.now()`, `new Date.getTime(), localTime.now()` all of thes give the same result and i try to use `JMH benchmark` but cannot running it.
公共BloomFilterIndex加密树(BloomFilterIndex树){
秒表表1=新秒表();
1.启动(“计算”);
对于(int m=0;mMicrobenchmarks)来说,很难获得正确的基准。您当前的方法不太可能产生有意义的基准。而且在这两种情况下,您都不应该使用Vector
。几天前您没有问过同样的问题吗?@gagansinghlol@Elliott弗里希这里有什么问题,我很困惑或者我该如何解决它。