java8stream与传统写法的性能对⽐简测 以下对⽐了⽤Stream完成去重复和从list<class>中提取其中⼀个属性的功能与传统的写法作对⽐, 看性能有否差异:java stream
两千万记录去重,⼀百万取属性:
static class Class1 {
public String staffId;
public String getStaffId() {
return staffId;
}
public String getStaffName() {
return staffName;
}
public String staffName;
public Class1(){}
public Class1(String staffId,String staffName){
this.staffId=staffId;
this.staffName=staffName;
}
public String toString(){
return "{id:"+staffId+",name:"+staffName+"}";
}
/*@Override
public int hashCode(){
return staffId.hashCode()+staffName.hashCode();
}*/
/*
@Override
public boolean equals(Object v){
Class1 a=(Class1) v;
return a.staffId.equals(this.staffId)&&a.staffName.equals(this.staffName);
}
@Override
public int hashCode(){
return staffId.hashCode()*staffName.hashCode();
}*/
}
static class ResultBean{
public List<Class1> result;
public long consume;
}
public ResultBean getUnique(List<Class1> pvData,Function<List<Class1>,List<Class1>> pvFun){
ResultBean lvRet=new ResultBean();
long lvTmStart=System.currentTimeMillis();
return lvRet;
}
static class ResultBean2{
public List<String> result;
public long consume;
}
public ResultBean2 getUnique2(List<Class1> pvData,Function<List<Class1>,List<String>> pvFun){
ResultBean2 lvRet=new ResultBean2();
long lvTmStart=System.currentTimeMillis();
return lvRet;
}
@Test
public void testRemoveDuplicate() {
List<String> lvList1 = new ArrayList<>();
lvList1.add("A");
lvList1.add("B");
lvList1.add("B");
lvList1.add("A");
lvList1.add("C");
lvList1.add("C");
lvList1 = lvList1.stream().distinct().List());
System.out.println(lvList1);
List<Class1> lvList2 = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
lvList2.add(new Class1("001", "Name001"));
lvList2.add(new Class1("002", "Name002"));
lvList2.add(new Class1("003", "Name003"));
lvList2.add(new Class1("001", "Name001"));
lvList2.add(new Class1("002", "Name002"));
lvList2.add(new Class1("003", "Name003"));
lvList2.add(new Class1("001", "Name001"));
lvList2.add(new Class1("002", "Name002"));
lvList2.add(new Class1("003", "Name003"));
lvList2.add(new Class1("003", "Name003"));
lvList2.add(new Class1("003", "Name003"));
lvList2.add(new Class1("004", "Name003"));
lvList2.add(new Class1("005", "Name003"));
lvList2.add(new Class1("004", "Name003"));
lvList2.add(new Class1("005", "Name003"));
lvList2.add(new Class1("004", "Name003"));
lvList2.add(new Class1("005", "Name003"));
lvList2.add(new Class1("003", "Name003"));
lvList2.add(new Class1("002", "Name003"));
lvList2.add(new Class1("002", "Name003"));
}
System.out.println(lvList2.size());
//lvList2=lvList2.stream().distinct().List());不起作⽤
ResultBean lvLambdaResult = getUnique(lvList2, p -> {
return lvList2.stream().llectingAndThen(
)
);
//return lvList2.stream().distinct().List());
});
System.out.println("Consume:" + sume);
System.out.println("result:" + sult);
ResultBean lvNormalResult = getUnique(lvList2, p -> {
TreeSet<Class1> lvTree = new TreeSet<Class1>(new Comparator<Class1>() {
@Override
public int compare(Class1 class1, Class1 t1) {
int lvRet = class1.staffIdpareTo(t1.staffId) * 100;
return lvRet + class1.staffNamepareTo(t1.staffName);
}
});
lvTree.addAll(p);
return Arrays.Array(new Class1[lvTree.size()]));
});
System.out.println("Consume:" + sume);
System.out.println("result:" + sult);
lvList2.clear();
for (int i = 0; i < 1000000; i++) {
lvList2.add(new Class1(i+"", "Name00"+i));
}
ResultBean2 lvLambdaResult2=getUnique2(lvList2,p->{
return p.stream().map(i->i.staffId).List());
});
System.out.println("Consume:"+sume);
ResultBean2 lvNormalResult2=getUnique2(lvList2,p->{
List<String> lvRet=new ArrayList<>();
for (Class1 rec:p){
lvRet.add(rec.staffId);
}
return lvRet;
});
System.out.println("Consume:"+sume);
}
结果如下, stream函数式的写法与传统写法性能要低些, 去重慢13%, 提取慢42%. 如果是在性能要求⽐较⾼的地⽅就要避免了.
20000000
Consume:806
result:[{id:001,name:Name001}, {id:002,name:Name002}, {id:002,name:Name003}, {id:003,name:Name003}, {id:004,name:Name003}, {id:005,name:Name003}] Consume:713
result:[{id:001,name:Name001}, {id:002,name:Name002}, {id:002,name:Name003}, {id:003,name:Name003}, {id:004,name:Name003}, {id:005,name:Name003}] Consume:30
Consume:21
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论