簡體   English   中英

兩種方法實現相同的算法,一種在java中比其他方法花費更多的運行時間

[英]Two methods implement same algorithm one is taking running time more than other in java

我正在嘗試使用new Date().getTime()計算兩種方法的運行時間。 兩種方法遵循相同的算法,其中一種方法增加了一些步驟,但步驟較少的方法需要更多時間。

我對此感到困惑。 這是兩種方法: 第一種方法使用較少的步驟並花費更多的時間:

public void encryptFiles(List<BloomFilter> bfList1) {
    Matrix matrix2 = new Matrix(400,400);
    Matrix matrix3 = new Matrix(400,400);

    matrix2.setMat(value1);
    matrix3.setMat(value2);

    a2 = matrix2.transpose();
    b2 = matrix3.transpose();

    startTime2 = new Date().getTime();
    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);

        //System.out.println("bloom filter in bloom filter list:" + Arrays.toString(bfList2.getBitSet().data));
        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 );
    }
    encryptedBFListInTime = (new Date().getTime())-startTime2;
    encryptedBFListInTime /= 1000.0; 
    System.out.println("encrypt files only in "+encryptedBFListInTime);
}

下面是第二種方法,使用更多的步驟但更少的時間:

public  BloomFilterIndex encryptTree(BloomFilterIndex tree) {

    startTime9 = new Date().getTime();
    for(int m = 0; m < tree.root.children.size(); m++){
        BloomFilterIndex.BFINode<Integer> n =(BloomFilterIndex.BFINode<Integer>)tree.root.children.get(m);
        encrypt(n);
    }
    end = new Date().getTime() - startTime9;
    //end1 = end - startTime9;
    end /= 1000.0;
    System.out.println("encrypt node 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()];
        // c = new double[4];
        for (int i = 0; i < root.value.size(); i++) {
        //  for(int i = 0; i < 4; 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 {
        //String bfName1 = root.value.getName(); 
        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++) {
            //  for(int i = 0;i < 400; 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;
        //  hasssh1.put(bfName1, ress);
    }
}

請問哪里有問題。

運行時間取決於代碼的臨界區 要確定臨界區,請記住它們包含在嵌套最深的 for 或 while 循環中。 其他代碼行只執行一次!

在第二種方法中,您從 for 循環中的WITHIN調用輔助方法! 這意味着您將在輔助方法tree.root.children.size()中執行所有for 循環和嵌套 for 循環,因為輔助方法被調用了很多次!

考慮嵌套循環時,乘以! 例如,

for (int i= 0; i < 5; i++) {
  for (int j= 0; j < 5; j++) {
    DOTHING();
  }
}

DOTHING 會執行 25 次! 為什么? 外循環執行5次! 嵌套循環執行 5 次! 5 x 5 = 25 次!

您從 for 循環中調用該輔助方法及其所有嵌套的 for 循環就像添加了另一個嵌套循環。 這就是 n * n 執行和 n * n * n 或 n^2 vs n^3 的區別! 我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM