在迭代中使用LambdaJ的效率严重缺乏 [英] Gross Lack of efficacy in using LambdaJ over iteration

查看:120
本文介绍了在迭代中使用LambdaJ的效率严重缺乏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a java ee application with JSF and JPA,I have to calculate totals of attributes in lists of objects. I have used LambdaJ library to achieve the calculation of sums.

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

As I use this sort of summations in several places of the application with possible implications to the overall performance, I designed the following test java application to test the efficacy of LambdaJ.

该申请证明了LambdaJ的功效严重不足.

This application demonstrated a gross lack of efficacy of LambdaJ.

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

Is there any error in the testing programme or should I replace all lambdaJ occurrences with iterations ?

结果

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;
    }


}

推荐答案

假设您的意思是效率而不是功效,那么您测得的效果或多或少都符合预期-请参见LambdaJ自己的性能分析.

Assuming you mean efficiency and not efficacy, then your measured performance is more or less as expected - see LambdaJ's own performance analysis.

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

You might get better results using Java 8 Lambda Expressions, but the main goal here is to improve readability and maintainability of source code and not speed. Are you sure that the time needed for performing the summations is critical to the overall performance of the application? I doubt it considering (I/O) waiting times for HTTP-traffic (JSF) and database queries (JPA) in which case this is premature optimization (the root of all evil).

这篇关于在迭代中使用LambdaJ的效率严重缺乏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆