java - 在迭代中使用 LambdaJ 严重缺乏效率

标签 java lambda lambdaj

在使用 JSF 和 JPA 的 java ee 应用程序中,我必须计算对象列表中的属性总数。我使用LambdaJ库来实现求和的计算。

由于我在应用程序的多个位置使用此类求和,可能会对整体性能产生影响,因此我设计了以下测试 Java 应用程序来测试 LambdaJ 的功效。

该应用程序证明 LambdaJ 严重缺乏功效。

测试程序中是否有任何错误,或者我应该用迭代替换所有出现的 lambdaJ 吗?

结果

Time taken to calculate a single total by iteration is 15 milliseconds.
Time taken to calculate a single total by LambdaJ is 199 milliseconds.
Time taken to calculate multiple totals by iteration is 23 milliseconds.
Time taken to calculate multiple totals by LambdaJ is 114 milliseconds.

主要方法

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lakmedi;

import ch.lambdaj.Lambda;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *
 * @author buddhika
 */
public class main {

    public static void main(String[] args) {
        List<Bill> bills = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 100000; i++) {
            Bill b = new Bill();
            b.setGrossTotal(r.nextDouble());
            b.setDiscount(r.nextDouble());
            b.setNetTotal(b.getGrossTotal() - b.getDiscount());
            bills.add(b);
        }
        calSingleByIteration(bills);
        calSingleByLambdja(bills);
        calMultipleByIteration(bills);
        calMultipleByLambdja(bills);
    }

    public static void calSingleByIteration(List<Bill> bills) {
        Date startTime = new Date();
        double grossTotal = 0.0;
        for (Bill b : bills) {
            grossTotal += b.getGrossTotal();
        }
        Date endTime = new Date();
        long timeTaken = endTime.getTime() - startTime.getTime();
        System.out.println("Time taken to calculate a single total by iteration is " + timeTaken + " milliseconds.");
    }

    public static void calSingleByLambdja(List<Bill> bills) {
        Date startTime = new Date();
        double grossTotal = Lambda.sumFrom(bills).getGrossTotal();
        Date endTime = new Date();
        long timeTaken = endTime.getTime() - startTime.getTime();
        System.out.println("Time taken to calculate a single total by LambdaJ is " + timeTaken + " milliseconds.");
    }

    public static void calMultipleByIteration(List<Bill> bills) {
        Date startTime = new Date();
        double grossTotal = 0.0;
        double discount = 0.0;
        double netTotal = 0.0;
        for (Bill b : bills) {
            grossTotal += b.getGrossTotal();
            discount += b.getDiscount();
            netTotal += b.getNetTotal();
        }
        Date endTime = new Date();
        long timeTaken = endTime.getTime() - startTime.getTime();
        System.out.println("Time taken to calculate multiple totals by iteration is " + timeTaken + " milliseconds.");
    }

    public static void calMultipleByLambdja(List<Bill> bills) {
        Date startTime = new Date();
        double grossTotal = Lambda.sumFrom(bills).getGrossTotal();
        double discount = Lambda.sumFrom(bills).getDiscount();
        double netTotal = Lambda.sumFrom(bills).getNetTotal();
        Date endTime = new Date();
        long timeTaken = endTime.getTime() - startTime.getTime();
        System.out.println("Time taken to calculate multiple totals by LambdaJ is " + timeTaken + " milliseconds.");
    }

}

比尔类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lakmedi;

/**
 *
 * @author buddhika
 */
public class Bill {
    private double grossTotal;
    private double netTotal;
    private double discount;

    public double getGrossTotal() {
        return grossTotal;
    }

    public void setGrossTotal(double grossTotal) {
        this.grossTotal = grossTotal;
    }

    public double getNetTotal() {
        return netTotal;
    }

    public void setNetTotal(double netTotal) {
        this.netTotal = netTotal;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }


}

最佳答案

假设你的意思是efficiency而不是efficacy ,那么您测得的性能或多或少符合预期 - 请参阅 LambdaJ 自己的 performance analysis .

使用 Java 8 可能会获得更好的结果 Lambda Expressions ,但这里的主要目标是提高源代码的可读性和可维护性,而不是速度。您确定执行求和所需的时间对于应用程序的整体性能至关重要吗?考虑到 HTTP 流量 (JSF) 和数据库查询 (JPA) 的 (I/O) 等待时间,我对此表示怀疑,在这种情况下,这是 premature optimization (万恶之源)。

关于java - 在迭代中使用 LambdaJ 严重缺乏效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25343897/

相关文章:

java - Spring 安全: deserialize request body twice (oauth2 processing)

java - 有没有办法让 Java lambda 表达式不引用封闭对象?

c# - 使用 Enumerable.Range() 填充字典时出现问题

java - Hamcrest - arrayOne 中的任何项目与 arrayTwo 中的任何项目匹配

java - 在 DefaultListCellRenderer 中格式化(展开)文本

java - double 到 int 和 double 到 int

java - 如何在 Java 中使用 BouncyCaSTLe API 对密码进行加密和加盐?

Java 8 for 循环不一致 : List of BinaryOperator vs List of Integer

java - 从 ARRAYLIST WITHOUT LOOP 中选择对象 - ANDROID

java - 过滤样本字符串中包含的项目