简体   繁体   English

是否有可能使此Java代码效率更高?

[英]Is it possible to make this java code far more efficient?

The code below is taking 27 seconds to load 51 images each image is about 22 KB (timed between the starting and ending alerts I inserted). 下面的代码花费27秒的时间来加载51张图像,每个图像大约22 KB(在我插入的开始和结束警报之间计时)。 Is it possible to make it a lot more efficient (I would like to get it to under 3 seconds)? 是否有可能使其效率更高(我希望将其缩短到3秒以内)?

At first I thought it was the database so I put the alerts in to make sure and found it was this code. 起初我以为是数据库,所以我把警报放进去以确保它是这段代码。

Regards, 问候,

Glyn 格林

public void renderYMAwardsTable(List<YouthMemberAwards> ymAwardsList) {
    if (!ymAwardsList.isEmpty()) {

        flexTableLink.clear();
        int linkRow = 0;
        int linkCol = 0;
        flexTableLeadership.clear();
        int leadershipRow = 0;
        int leadershipCol = 0;
        flexTableBoomerang.clear();
        int boomerangRow = 0;
        int boomerangCol = 0;
        flexTableAchievement.clear();
        int achievementRow = 0;
        int achievementCol = 0;
        flexTableSpecialInterest.clear();
        int specialInterestRow = 0;
        int specialInterestCol = 0;
        totalAwards = 0;


        Window.alert("Start populating page.");
        for (final YouthMemberAwards ymAwards : ymAwardsList) {
            // Display awards and, if applicable, date awarded

            if (((ymAwards.getCaAwardedDate() != null)
                    || (ymAwards.getAwArchivedDate() == null)
                    || (ymAwards.getCaAwardStarted().equals("Y"))
                    ) && (ymAwards.getAwStartedDate().before(ymEndDate))) {

                //Display each award in the correct area with:
                //  the date awarded, if applicable, and
                //  the date box shaded if the award has been started but not awarded

                String imageDataString = ymAwards.getAwAwardPicture();
                Image image = new Image(imageDataString);
                image.setWidth("75px");
                image.setHeight("75px");
                image.setStyleName("gwt-Selectable");

                final DateBox awardedDate = new DateBox();
                awardedDate.setFormat(new DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
                awardedDate.setValue(ymAwards.getCaAwardedDate());
                awardedDate.setWidth("75px");
                awardedDate.setFireNullValues(true);                    
                //Check if the Youth Member has started the Award,
                //if they have then colour the date box Green.
                if (ymAwards.getCaAwardStarted()!= null){
                    if ((ymAwards.getCaAwardedDate() == null)
                            && (ymAwards.getCaAwardStarted().equals("Y"))){
                        awardedDate.setStyleName("gwt-Green-Background");
                    }else{
                        awardedDate.setStyleName("gwt-Label-Login");
                    }
                }else{
                    awardedDate.setStyleName("gwt-Label-Login");
                }

                //Tally the number of Awards the Youth Member has been awarded.
                if (ymAwards.getCaAwardedDate() != null){
                    totalAwards = totalAwards + 1;
                }

                //Display each Award in the appropriate category.
                if (ymAwards.getAwAwardType().equals("Link")){

                    flexTableLink.setWidget(linkRow, linkCol, image);
                    flexTableLink.setWidget(linkRow + 1, linkCol, awardedDate);
                    if (linkCol < 10){
                        linkCol = linkCol + 1;
                    }else{
                        linkCol = 0;
                        linkRow = linkRow + 2;
                    }

                }else{
                    if (ymAwards.getAwAwardType().equals("Leadership")){

                        flexTableLeadership.setWidget(leadershipRow, leadershipCol, image);
                        flexTableLeadership.setWidget(leadershipRow + 1, leadershipCol, awardedDate);
                        if (leadershipCol < 10){
                            leadershipCol = leadershipCol + 1;
                        }else{
                            leadershipCol = 0;
                            leadershipRow = leadershipRow + 2;
                        }

                    }else{
                        if (ymAwards.getAwAwardType().equals("Boomerang")){

                            flexTableBoomerang.setWidget(boomerangRow, boomerangCol, image);
                            flexTableBoomerang.setWidget(boomerangRow + 1, boomerangCol, awardedDate);
                            if (boomerangCol < 10){
                                boomerangCol = boomerangCol + 1;
                            }else{
                                boomerangCol = 0;
                                boomerangRow = boomerangRow + 2;
                            }

                        }else{
                            if (ymAwards.getAwAwardType().equals("Achievement")){

                                flexTableAchievement.setWidget(achievementRow, achievementCol, image);
                                flexTableAchievement.setWidget(achievementRow + 1, achievementCol, awardedDate);
                                if (achievementCol < 10){
                                    achievementCol = achievementCol + 1;
                                }else{
                                    achievementCol = 0;
                                    achievementRow = achievementRow + 2;
                                }

                            }else{
                                if (ymAwards.getAwAwardType().equals("Special Interest")){

                                    flexTableSpecialInterest.setWidget(specialInterestRow, specialInterestCol, image);
                                    flexTableSpecialInterest.setWidget(specialInterestRow + 1, specialInterestCol, awardedDate);
                                    if (specialInterestCol < 10){
                                        specialInterestCol = specialInterestCol + 1;
                                    }else{
                                        specialInterestCol = 0;
                                        specialInterestRow = specialInterestRow + 2;
                                    }

                                }else{
                                    //If not found then default to Special Interest.

                                    flexTableSpecialInterest.setWidget(specialInterestRow, specialInterestCol, image);
                                    flexTableSpecialInterest.setWidget(specialInterestRow + 1, specialInterestCol, awardedDate);
                                    if (specialInterestCol < 10){
                                        specialInterestCol = specialInterestCol + 1;
                                    }else{
                                        specialInterestCol = 0;
                                        specialInterestRow = specialInterestRow + 2;
                                    }

                                }
                            }
                        }
                    }
                }

                //Add a click handler to the image
                image.addClickHandler(new ClickHandler(){

                    public void onClick(ClickEvent event){
                        //Store the data from this view for use in subsequent Views (ScoutAwardView).
                        AsyncCallback<ViewData> callback = new ViewDataStoreHandler<ViewData>();
                        rpc.setViewData(accountId, accountLevel, youthMemberID, ymAwards.getAwId(), "0", callback);

                        //If the Award has sub groups then display the Groups and allow one the 
                        //be selected to display the details. Otherwise, display the details.
                        if (ymAwards.getAwGrouped().equals("Y")) {
                            //Go to the AwardGroupView
                            navHandler2.go("AwardGroup");
                        }else{
                            //Go to the ScoutAwardView
                            navHandler2.go("ScoutAward");
                        }

                    }
                });

                //Add change handler for the awarded date.
                //Only a Leader or Administrator can update the date
                if (accountLevel.equals("Leader") || accountLevel.equals("Administrator")) {
                    awardedDate.addValueChangeHandler(new ValueChangeHandler<java.util.Date>() {
                        public void onValueChange(ValueChangeEvent<java.util.Date> event) {
                            //Check for a null date and handle it for dateBoxArchived and dateBoxPackOut
                            java.sql.Date sqlDateAwarded = awardedDate.getValue() == null ? null : new java.sql.Date(awardedDate.getValue().getTime());
                            AsyncCallback<Void> callback = new YMAwardedDateHandler<Void>();
                            rpc.updateYMAwarded(youthMemberID, ymAwards.getAwId(), sqlDateAwarded, callback);

                            @SuppressWarnings("unused")
                            AdjustAwardStock adjustAwardStock = new AdjustAwardStock(sqlDateAwarded, ymAwards.getAwId());
                        }
                    });
                }
            }
        }
    }
    Window.alert("Finish populating page.");
    //Hide "Loading please wait" popup. 
    popup.hide();

    //Display the number of Awards earned.
    String totalAwardsString = Integer.toString(totalAwards);
    textBoxTotalAwards.setText(totalAwardsString);
}

First of all, an image with 75x75 pixels size should not be 22kB. 首先,75x75像素大小的图像不应为22kB。 Even a PNG-24 image of this size is about 3kB, let alone GIF. 即使是这种大小的PNG-24图像也大约为3kB,更不用说GIF了。 Store your images in the correct sizes and the appropriate file format (use PNG or GIF). 以正确的尺寸和适当的文件格式(使用PNG或GIF)存储图像。 For 51 images that means a difference between 1MB and 150kB. 对于51张图像,这意味着1MB和150kB之间的差异。 This is the first big improvement. 这是第一个重大改进。

Second, if you use a limited number of images, combine them into a single sprite. 其次,如果使用有限数量的图像,请将它们组合为一个精灵。 This will reduce the number of round-trip server calls from 51 (in your example) to 1. That's another huge improvement. 这会将往返服务器调用的次数从51(在您的示例中)减少到1。这是另一个巨大的改进。

You don't need to make this sprite manually (unless you want to). 您无需手动制作此精灵(除非您愿意)。 You can use GWT's ImageResource ClientBundle . 您可以使用GWT的ImageResource ClientBundle Note that this sprite can be cached, so a browser won't need to load it every time a user visits this page. 请注意,可以缓存此精灵,因此浏览器不需要在用户每次访问此页面时加载它。

The other suggestions (like using switch statements) are good for code readability and maintenance, but they won't give you a significant performance boost because your Java code is compiled into JavaScript, and the compiler is pretty smart. 其他建议(例如使用switch语句)对于代码的可读性和维护性很有好处,但是由于您的Java代码已编译为JavaScript,并且编译器非常智能,因此它们不会显着提高性能。

For combining the images and other JS files, you can always use gzip functionality supported by browser so that all the files can b zipped and send to the browser to unzip. 为了合并图像和其他JS文件,您始终可以使用浏览器支持的gzip功能,以便可以压缩所有文件并将其发送到浏览器以解压缩。 It will lot of network time and transferred data size will b much reduced. 这将花费大量的网络时间,并且传输的数据大小将大大减少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM