简体   繁体   中英

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). Is it possible to make it a lot more efficient (I would like to get it to under 3 seconds)?

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. Even a PNG-24 image of this size is about 3kB, let alone GIF. Store your images in the correct sizes and the appropriate file format (use PNG or GIF). For 51 images that means a difference between 1MB and 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.

You don't need to make this sprite manually (unless you want to). You can use GWT's 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.

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. It will lot of network time and transferred data size will b much reduced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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