Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Cicles改善/降低性能的方面_Java_Performance_Loops_Functional Programming_Structured Programming - Fatal编程技术网

Java Cicles改善/降低性能的方面

Java Cicles改善/降低性能的方面,java,performance,loops,functional-programming,structured-programming,Java,Performance,Loops,Functional Programming,Structured Programming,我一直在对可变和不可变列表进行性能检查。我创建了以下测试。测试非常简单,我想知道应该考虑哪些方面来改进lambda表达式,以获得最接近或更好的for循环性能 现在,我列出了其中一些: 排序列表将提高性能 Inmutable集合还可以提高性能 使用可选选项会降低性能 其他方面会影响到什么 增强的已用时间:141 已用时间:109 迭代器-运行时间:125 流-运行时间:172 并行流-运行时间:94 可选流-运行时间:156 可选并行流-运行时间:140 import java.util.Arra

我一直在对可变和不可变列表进行性能检查。我创建了以下测试。测试非常简单,我想知道应该考虑哪些方面来改进lambda表达式,以获得最接近或更好的for循环性能

现在,我列出了其中一些:

  • 排序列表将提高性能
  • Inmutable集合还可以提高性能
  • 使用可选选项会降低性能
  • 其他方面会影响到什么

    增强的已用时间:141

    已用时间:109

    迭代器-运行时间:125

    流-运行时间:172

    并行流-运行时间:94

    可选流-运行时间:156

    可选并行流-运行时间:140

    import java.util.ArrayList;
    导入java.util.Collections;
    导入java.util.Iterator;
    导入java.util.List;
    导入java.util.Objects;
    导入java.util.Optional;
    导入java.util.concurrent.ThreadLocalRandom;
    导入java.util.stream.collector;
    类MyObject{
    私有整数id;
    私有字符串名称;
    私有字符串消息;
    私有MySecondObject对象;
    公共MyObject(整数id、字符串名称、字符串消息、MySecondObject对象){
    this.id=id;
    this.name=名称;
    this.message=消息;
    this.object=对象;
    }
    公共整数getId(){
    返回id;
    }
    公共无效集合id(整数id){
    this.id=id;
    }
    公共字符串getName(){
    返回名称;
    }
    公共void集合名(字符串名){
    this.name=名称;
    }
    公共字符串getMessage(){
    返回消息;
    }
    公共无效设置消息(字符串消息){
    this.message=消息;
    }
    public MySecondObject getObject(){
    返回对象;
    }
    public void setObject(MySecondObject对象){
    this.object=对象;
    }
    @凌驾
    公共int hashCode(){
    int hash=8;
    hash=14*hash+Objects.hashCode(this.id);
    hash=14*hash+Objects.hashCode(this.name);
    hash=14*hash+Objects.hashCode(this.message);
    hash=14*hash+Objects.hashCode(this.object);
    返回散列;
    }
    @凌驾
    公共布尔等于(对象obj){
    if(this==obj){
    返回true;
    }
    if(obj==null){
    返回false;
    }
    如果(getClass()!=obj.getClass()){
    返回false;
    }
    最终MyObject other=(MyObject)obj;
    如果(!Objects.equals(this.name,other.name)){
    返回false;
    }
    如果(!Objects.equals(this.message,other.message)){
    返回false;
    }
    如果(!Objects.equals(this.id,other.id)){
    返回false;
    }
    返回Objects.equals(this.object,other.object);
    }
    @凌驾
    公共字符串toString(){
    返回“\nMyObject{”+“id=“+id+”,name=“+name+”,message=“+message+”,object=“+object+'}”;
    }
    }
    类MySecondObject{
    私有整数id;
    私有字符串名称;
    私有字符串消息;
    公共MySecondObject(整数id、字符串名称、字符串消息){
    this.id=id;
    this.name=名称;
    this.message=消息;
    }
    公共整数getId(){
    返回id;
    }
    公共无效集合id(整数id){
    this.id=id;
    }
    公共字符串getName(){
    返回名称;
    }
    公共void集合名(字符串名){
    this.name=名称;
    }
    公共字符串getMessage(){
    返回消息;
    }
    公共无效设置消息(字符串消息){
    this.message=消息;
    }
    @凌驾
    公共int hashCode(){
    int hash=7;
    hash=13*hash+Objects.hashCode(this.id);
    hash=13*hash+Objects.hashCode(this.name);
    hash=13*hash+Objects.hashCode(this.message);
    返回散列;
    }
    @凌驾
    公共布尔等于(对象obj){
    if(this==obj){
    返回true;
    }
    if(obj==null){
    返回false;
    }
    如果(getClass()!=obj.getClass()){
    返回false;
    }
    最终MySecondObject other=(MySecondObject)obj;
    如果(!Objects.equals(this.name,other.name)){
    返回false;
    }
    如果(!Objects.equals(this.message,other.message)){
    返回false;
    }
    返回Objects.equals(this.id,other.id);
    }
    @凌驾
    公共字符串toString(){
    返回“MySecondObject{”+“id=“+id+”,name=“+name+”,message=“+message+'}”;
    }
    }
    类MyObject结构{
    私有MyObjectUtil(){
    }
    公共静态列表getList(){
    List myObjectList=new ArrayList();
    final ThreadLocalRandom r=ThreadLocalRandom.current();
    对于(int i=0;i<1_000;i++){
    添加(新的MyObject(r.nextInt(),“Name”+i,“Message”+i,新的MySecondObject(r.nextInt(),“Second Object”+i,“Message”+i));
    }
    添加(新的MyObject(r.nextInt(),“Name”+0,“Message”+0,null));
    myObjectList.sort((a,b)->a.getId().equals(b.getId())?0:a.getId()>b.getId()?1:-1);
    返回集合。不可修改列表(myObjectList);
    //返回myObjectList;
    }
    }
    公共类MyObjectListTest{
    公共静态void main(字符串[]args){
    List myObjectList=(List)MyObjectUtil.getList();
    long begin=System.currentTimeMillis();
    List idList=new ArrayList();
    if(myObjectList!=null){
    用于(MyObject对象:MyObject列表){
    if(object!=null&&object.getObject()!=null){
    add(object.getObject().getId());
    }
    }
    }
    长端=System.currentTimeMi
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Objects;
    import java.util.Optional;
    import java.util.concurrent.ThreadLocalRandom;
    import java.util.stream.Collectors;
    
    class MyObject {
        private Integer id;
        private String name;
        private String message;
        private MySecondObject object;
        public MyObject(Integer id, String name, String message, MySecondObject object) {
            this.id = id;
            this.name = name;
            this.message = message;
            this.object = object;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public MySecondObject getObject() {
            return object;
        }
        public void setObject(MySecondObject object) {
            this.object = object;
        }
        @Override
        public int hashCode() {
            int hash = 8;
            hash = 14 * hash + Objects.hashCode(this.id);
            hash = 14 * hash + Objects.hashCode(this.name);
            hash = 14 * hash + Objects.hashCode(this.message);
            hash = 14 * hash + Objects.hashCode(this.object);
            return hash;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final MyObject other = (MyObject) obj;
            if (!Objects.equals(this.name, other.name)) {
                return false;
            }
            if (!Objects.equals(this.message, other.message)) {
                return false;
            }
            if (!Objects.equals(this.id, other.id)) {
                return false;
            }
            return Objects.equals(this.object, other.object);
        }
        @Override
        public String toString() {
            return "\nMyObject{" + "id=" + id + ", name=" + name + ", message=" + message + ", object=" + object + '}';
        }
    }
    
    class MySecondObject {
        private Integer id;
        private String name;
        private String message;
        public MySecondObject(Integer id, String name, String message) {
            this.id = id;
            this.name = name;
            this.message = message;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        @Override
        public int hashCode() {
            int hash = 7;
            hash = 13 * hash + Objects.hashCode(this.id);
            hash = 13 * hash + Objects.hashCode(this.name);
            hash = 13 * hash + Objects.hashCode(this.message);
            return hash;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final MySecondObject other = (MySecondObject) obj;
            if (!Objects.equals(this.name, other.name)) {
                return false;
            }
            if (!Objects.equals(this.message, other.message)) {
                return false;
            }
            return Objects.equals(this.id, other.id);
        }
        @Override
        public String toString() {
            return "MySecondObject{" + "id=" + id + ", name=" + name + ", message=" + message + '}';
        }
    }
    
    class MyObjectUtil {
        private MyObjectUtil() {
        }
        public static List<?> getList() {
            List<MyObject> myObjectList = new ArrayList<>();
            final ThreadLocalRandom r = ThreadLocalRandom.current();
            for (int i = 0; i < 1_000_000; i++) {
                myObjectList.add(new MyObject(r.nextInt(), "Name " + i, "Message " + i, new MySecondObject(r.nextInt(), "Second Object " + i, "Message " + i)));
            }
            myObjectList.add(new MyObject(r.nextInt(), "Name " + 0, "Message " + 0, null));
            myObjectList.sort((a, b) -> a.getId().equals(b.getId()) ? 0 : a.getId()> b.getId()? 1 : -1);
            return Collections.unmodifiableList(myObjectList);
            //return myObjectList;
        }
    }
    
    public class MyObjectListTest {
    
        public static void main(String[] args) {
            List<MyObject> myObjectList = (List<MyObject>) MyObjectUtil.getList();
    
            long begin = System.currentTimeMillis();
            List<Integer> idList = new ArrayList<>();
            if (myObjectList != null) {
                for (MyObject object : myObjectList) {
                    if (object != null && object.getObject() != null) {
                        idList.add(object.getObject().getId());
                    }
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("\nEnhanced For - Elapsed time: " + (end - begin));
    
            idList = null;
            begin = System.currentTimeMillis();
            idList = new ArrayList<>();
            if (myObjectList != null) {
                for (int i = 0; i < myObjectList.size(); i++) {
                    MyObject o = myObjectList.get(i);
                    if (o != null && o.getObject() != null) {
                        idList.add(o.getObject().getId());
                    }
                }
            }
            end = System.currentTimeMillis();
            System.out.println("\nFor - Elapsed time: " + (end - begin));
    
            idList = null;
            begin = System.currentTimeMillis();
            idList = new ArrayList<>();
            if (myObjectList != null) {
                for (Iterator<MyObject> it = myObjectList.iterator(); it.hasNext();) {
                    MyObject m = it.next();
                    if (m.getObject() != null) {
                        idList.add(m.getObject().getId());
                    }
                }
            }
            end = System.currentTimeMillis();
            System.out.println("\nIterator - Elapsed time: " + (end - begin));
    
            idList = null;
            begin = System.currentTimeMillis();
            if (myObjectList != null) {
                idList = myObjectList
                        .stream()
                        .map(MyObject::getObject)
                        .filter(Objects::nonNull)
                        .map(MySecondObject::getId)
                        .collect(Collectors.toList());
            }
            end = System.currentTimeMillis();
            System.out.println("\nStream - Elapsed time: " + (end - begin));
    
            idList = null;
            begin = System.currentTimeMillis();
            if (myObjectList != null) {
                idList = myObjectList
                        .parallelStream()
                        .map(MyObject::getObject)
                        .filter(Objects::nonNull)
                        .map(MySecondObject::getId)
                        .collect(Collectors.toList());
            }
            end = System.currentTimeMillis();
            System.out.println("\nParallel Stream - Elapsed time: " + (end - begin));
    
            idList = null;
            begin = System.currentTimeMillis();
            idList = Optional.ofNullable(myObjectList)
                    .orElseGet(Collections::emptyList)
                    .stream()
                    .map(MyObject::getObject)
                    .filter(Objects::nonNull)
                    .map(MySecondObject::getId)
                    .collect(Collectors.toList());
            end = System.currentTimeMillis();
            System.out.println("\nOptional Stream - Elapsed time: " + (end - begin));
    
            idList = null;
            begin = System.currentTimeMillis();
            idList = Optional.ofNullable(myObjectList)
                    .orElseGet(Collections::emptyList)
                    .parallelStream()
                    .map(MyObject::getObject)
                    .filter(Objects::nonNull)
                    .map(MySecondObject::getId)
                    .collect(Collectors.toList());
            end = System.currentTimeMillis();
            System.out.println("\nOptional Parallel Stream - Elapsed time: " + (end - begin));
        }
    }