是否有可能使这个java代码更有效率?

时间:2014-05-21 02:09:00

标签: java eclipse gwt

以下代码需要27秒才能加载51张图像,每张图像约为22 KB(我插入的开始和结束警报之间的时间间隔)。是否有可能使它更有效率(我想在3秒内达到它)?

起初我以为是数据库所以我把警报放进去确认并发现它是这个代码。

此致

格林

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

2 个答案:

答案 0 :(得分:6)

首先,尺寸为75x75像素的图像不应为22kB。即使是这种尺寸的PNG-24图像也只有3kB,更不用说GIF了。以正确的尺寸和相应的文件格式存储图像(使用PNG或GIF)。对于51个图像,这意味着1MB到150kB之间的差异。这是第一次重大改进。

其次,如果您使用有限数量的图像,请将它们组合成一个精灵。这会将往返服务器调用的次数从51(在您的示例中)减少到1.这是另一个巨大的改进。

您不需要手动制作此精灵(除非您愿意)。您可以使用GWT ImageResource ClientBundle。请注意,此精灵可以缓存,因此每次用户访问此页面时,浏览器都不需要加载它。

其他建议(比如使用switch语句)有利于代码的可读性和维护,但是它们不会给你带来显着的性能提升,因为你的Java代码被编译成JavaScript,并且编译器非常聪明。 / p>

答案 1 :(得分:0)

对于组合图像和其他JS文件,您始终可以使用浏览器支持的gzip功能,以便所有文件都可以压缩并发送到浏览器进行解压缩。它将大量的网络时间和转移的数据大小将大大减少。