when implement two methods follow same algorithm , i want to calculate running time for each. but i notice that the method with less code take time more than other method. the following is the first method with less code but take more running time:
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);
}
the other method with the same code and some extra steps but less running time:
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.