码迷,mamicode.com
首页 > 编程语言 > 详细

算法结构的微小差异引起结构和性能上的差异性

时间:2016-05-10 12:35:10      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

两个方法都是做盘点数据与库存数据的比较,method2为修改后方法,两个方法结构调整的意义,是否带来结构上的优化和性能上的提升?

if(){}else{}的嵌套使用相较于if(){},if(){}结构上的优劣又体现在哪里?

Method1:

/**
     * 一次盘点与库存比较
     * 
     * @param list盘点数据集
     * @param checkId
     * @return listReault差异结果集
     */
    private List<StorageCheckDiffReaultDetail> compareFirstDetail2(int checkId){
        StorageCheck storageCheck = storageCheckDao.findById(checkId);
        if(storageCheck==null){
            throw new RuntimeException("盘点id不存在!!!");
        }
        Integer positionId=storageCheck.getPositionId();
        // 比较结果
        List<StorageCheckDiffReaultDetail> listResault = new ArrayList<StorageCheckDiffReaultDetail>();
        // 把库存封装成StorageCheckSnapDataDetail对象列表
        List<StorageCheckSnapDataDetail> listPosDetails = processGetListPosDetail(positionId);
        // 把快照数据封装成StorageCheckSnapDataDetail对象列表
        List<StorageCheckSnapDataDetail> listSnapDetails = processFindListSnapDetail(checkId);
        //排序
        Collections.sort(listPosDetails);
        Collections.sort(listSnapDetails);
        int j=0;
        int i=0;
        for (;;) {
            if(i>=listSnapDetails.size()&&j>=listPosDetails.size()){//不存在数据结束循环
                break;
            }
            Integer posSkuId=listPosDetails.get(j).getSkuId();
            Integer posNum=listPosDetails.get(j).getSnapNum();
            if(i>=listSnapDetails.size()&&j<listPosDetails.size()){//只剩库存数据
                StorageCheckDiffReaultDetail diffData=
                        new StorageCheckDiffReaultDetail(posSkuId, 0, posNum);
                listResault.add(diffData);
                j++;
                continue;
            }
            Integer snapSkuId=listSnapDetails.get(i).getSkuId();
            Integer snapNum=listSnapDetails.get(i).getSnapNum();
            if(j>=listPosDetails.size()&&i<listSnapDetails.size()){//只剩真实盘点数据
                StorageCheckDiffReaultDetail diffData=
                        new StorageCheckDiffReaultDetail(snapSkuId, snapNum, 0);
                listResault.add(diffData);
                i++;
                continue;
            }
            
            if(snapSkuId.equals(posSkuId)){//skuId相同,
                if(snapNum.equals(posNum)){//数量相同
                }else{//数量不同
                    StorageCheckDiffReaultDetail diffData=
                            new StorageCheckDiffReaultDetail(snapSkuId, snapNum, posNum);
                    listResault.add(diffData);
                }
                i++;
                j++;
                continue;
            }else{//skuId不同
                if(snapSkuId<posSkuId){//真实数据存在,库存不存在
                    StorageCheckDiffReaultDetail diffData=
                            new StorageCheckDiffReaultDetail(snapSkuId, snapNum, 0);
                    listResault.add(diffData);
                    i++;
                    continue;
                }else{//真实数据不存在,库存存在
                    StorageCheckDiffReaultDetail diffData=
                            new StorageCheckDiffReaultDetail(posSkuId, 0, posNum);
                    listResault.add(diffData);
                    j++;
                    continue;
                }
            }
        }
        return listResault;
    }
    

Method2:

/**
     * 一次盘点与库存比较
     * 
     * @param list盘点数据集
     * @param checkId
     * @return listReault差异结果集
     */
    private List<StorageCheckDiffReaultDetail> compareFirstDetail(int checkId){
        StorageCheck storageCheck = storageCheckDao.findById(checkId);
        if(storageCheck==null){
            throw new RuntimeException("盘点id不存在!!!");
        }
        Integer positionId=storageCheck.getPositionId();
        // 比较结果
        List<StorageCheckDiffReaultDetail> listResault = new ArrayList<StorageCheckDiffReaultDetail>();
        // 把库存封装成StorageCheckSnapDataDetail对象列表
        List<StorageCheckSnapDataDetail> listPosDetails = processGetListPosDetail(positionId);
        // 把快照数据封装成StorageCheckSnapDataDetail对象列表
        List<StorageCheckSnapDataDetail> listSnapDetails = processFindListSnapDetail(checkId);
        //排序
        Collections.sort(listPosDetails);
        Collections.sort(listSnapDetails);
        int j=0;
        int i=0;
        while(i<listSnapDetails.size()&&j<listPosDetails.size()) {
            Integer posSkuId=listPosDetails.get(j).getSkuId();
            Integer posNum=listPosDetails.get(j).getSnapNum();
            Integer snapSkuId=listSnapDetails.get(i).getSkuId();
            Integer snapNum=listSnapDetails.get(i).getSnapNum();
            // skuId和数量都相同
            if(snapSkuId.equals(posSkuId) && snapNum.equals(posNum)){
                i++;
                j++;
                continue;
            }
            // skuId相同但数量不同
            if (snapSkuId.equals(posSkuId) && !snapNum.equals(posNum)) {
                listResault.add(new StorageCheckDiffReaultDetail(snapSkuId, snapNum, posNum));
                i++;
                j++;
                continue;
            }
            // skuId不同:真实数据存在,库存不存在
            if (snapSkuId < posSkuId) {// 真实数据存在,库存不存在
                listResault.add(new StorageCheckDiffReaultDetail(snapSkuId, snapNum, 0));
                i++;
                continue;
            }
            // skuId不同:真实数据不存在,库存存在
            listResault.add(new StorageCheckDiffReaultDetail(posSkuId, 0, posNum));
            j++;
            continue;
        };
        List<StorageCheckSnapDataDetail> listSnap;
        // 如果快照有剩余记录,全部添加到listResults中
        if(i<listSnapDetails.size()) {
            listSnap= listSnapDetails.subList(i, listSnapDetails.size());
            for (StorageCheckSnapDataDetail dataDetail : listSnap) {
                listResault.add(new StorageCheckDiffReaultDetail(dataDetail.getSkuId(), dataDetail.getSnapNum(), 0));
            }
        }
        // 如果实际库存有剩余记录,全部添加到listResults中
        if(j<listPosDetails.size()) {
            //listResault.addAll(restCheckList(listPosDetails, j));
            listSnap= listPosDetails.subList(i, listPosDetails.size());
            for (StorageCheckSnapDataDetail dataDetail : listSnap) {
                listResault.add(new StorageCheckDiffReaultDetail(dataDetail.getSkuId(), 0,dataDetail.getSnapNum()));
            }
        }
        return listResault;
    }

 

算法结构的微小差异引起结构和性能上的差异性

标签:

原文地址:http://www.cnblogs.com/NextNight/p/5477304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!