lang/java

java filter speed Test

C/H 2021. 12. 31. 02:09
import java.util.*;
import java.util.stream.Collectors;

public class helloWorld {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        for(int i = 0; i <= 99999; i++){
            students.add(new Student("A", i, i % 3 == 0 ? "M" : "F"));
            students.add(new Student("B", i, i % 3 == 0 ? "M" : "F"));
            students.add(new Student("C", i, i % 3 == 0 ? "M" : "F"));
        }

        System.out.println("sutudents size: " + students.size());

        List<Student> listA1 = new ArrayList<>(),
                listB1 = new ArrayList<>(),
                listC1 = new ArrayList<>();
        long begin = System.nanoTime();
        for (Student student : students) {
            String name = student.getName();
            if (name.equals("A")) listA1.add(student);
            else if (name.equals("B")) listB1.add(student);
            else if (name.equals("C")) listC1.add(student);
        }
        System.out.println("EliipsesTime01: " + (System.nanoTime() - begin) + " ns");
        System.out.println("A1.size: " + listA1.size() + ", B1.size: " + listB1.size() + ", listC1.size: " + listC1.size());

        List<Student> listA2 = new ArrayList<>(),
                listB2 = new ArrayList<>(),
                listC2 = new ArrayList<>();
        long begin2 = System.nanoTime();
        for (Student student : students) {
            switch (student.getName()) {
                case "A": listA2.add(student);break;
                case "B": listB2.add(student);break;
                case "C": listC2.add(student);break;
                default: break;
            }
        }
        System.out.println("EliipsesTime02: " + (System.nanoTime() - begin2) + " ns");
        System.out.println("A2.size: " + listA2.size() + ", B2.size: " + listB2.size() + ", listC2.size: " + listC2.size());

        long begin11 = System.nanoTime();
        List<Student> listA11 = students.stream().filter(o -> o.getName().equals("A")).collect(Collectors.toList());
        List<Student> listB11 = students.stream().filter(o -> o.getName().equals("B")).collect(Collectors.toList());
        List<Student> listC11 = students.stream().filter(o -> o.getName().equals("C")).collect(Collectors.toList());
        System.out.println("EliipsesTime11: " + (System.nanoTime() - begin11) + " ns");
        System.out.println("A11.size: " + listA11.size() + ", B11.size: " + listB11.size() + ", listC11.size: " + listC11.size());

        long begin12 = System.nanoTime();
        List<Student> listA12 = students.parallelStream().filter(o -> o.getName().equals("A")).collect(Collectors.toList());
        List<Student> listB12 = students.parallelStream().filter(o -> o.getName().equals("B")).collect(Collectors.toList());
        List<Student> listC14 = students.parallelStream().filter(o -> o.getName().equals("C")).collect(Collectors.toList());
        System.out.println("EliipsesTime12: " + (System.nanoTime() - begin12) + " ns");
        System.out.println("A12.size: " + listA12.size() + ", B12.size: " + listB12.size() + ", listC12.size: " + listC14.size());
    }
}

class Student {
    private String name = "";
    private int age = 0;
    private String sex = "";

    public Student(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -javaagent:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=41779:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/java-atk-wrapper.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jfr.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar:/home/uncaose/Projects/Java/helloWorld/out/production/Java helloWorld
sutudents size: 300000
EliipsesTime01: 13322717 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime02: 14319003 ns
A2.size: 100000, B2.size: 100000, listC2.size: 100000
EliipsesTime11: 70470831 ns
A11.size: 100000, B11.size: 100000, listC11.size: 100000
EliipsesTime12: 29156341 ns
A12.size: 100000, B12.size: 100000, listC12.size: 100000

Process finished with exit code 0


/usr/lib/jvm/java-8-openjdk-amd64/bin/java -javaagent:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=33691:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/java-atk-wrapper.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jfr.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar:/home/uncaose/Projects/Java/helloWorld/out/production/Java helloWorld
sutudents size: 300000
EliipsesTime01: 19656517 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime02: 12744903 ns
A2.size: 100000, B2.size: 100000, listC2.size: 100000
EliipsesTime11: 71352798 ns
A11.size: 100000, B11.size: 100000, listC11.size: 100000
EliipsesTime12: 28338458 ns
A12.size: 100000, B12.size: 100000, listC12.size: 100000

Process finished with exit code 0
/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -javaagent:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=40327:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/uncaose/Projects/Java/helloWorld/out/production/Java helloWorld
sutudents size: 300000
EliipsesTime01: 11519402 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime02: 10697319 ns
A2.size: 100000, B2.size: 100000, listC2.size: 100000
EliipsesTime11: 41825493 ns
A11.size: 100000, B11.size: 100000, listC11.size: 100000
EliipsesTime12: 34319151 ns
A12.size: 100000, B12.size: 100000, listC12.size: 100000

Process finished with exit code 0

/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -javaagent:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=42073:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/uncaose/Projects/Java/helloWorld/out/production/Java helloWorld
sutudents size: 300000
EliipsesTime01: 12191750 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime02: 18121540 ns
A2.size: 100000, B2.size: 100000, listC2.size: 100000
EliipsesTime11: 42957843 ns
A11.size: 100000, B11.size: 100000, listC11.size: 100000
EliipsesTime12: 53593287 ns
A12.size: 100000, B12.size: 100000, listC12.size: 100000

Process finished with exit code 0
/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -javaagent:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=43265:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/uncaose/Projects/Java/helloWorld/out/production/Java helloWorld
sutudents size: 300000
EliipsesTime01: 12025406 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime02: 13400851 ns
A2.size: 100000, B2.size: 100000, listC2.size: 100000
EliipsesTime11: 42708709 ns
A11.size: 100000, B11.size: 100000, listC11.size: 100000
EliipsesTime12: 16916257 ns
A12.size: 100000, B12.size: 100000, listC12.size: 100000

Process finished with exit code 0

/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -javaagent:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=44303:/home/uncaose/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/uncaose/Projects/Java/helloWorld/out/production/Java helloWorld
sutudents size: 300000
EliipsesTime01: 10287032 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime02: 9839069 ns
A2.size: 100000, B2.size: 100000, listC2.size: 100000
EliipsesTime11: 45944234 ns
A11.size: 100000, B11.size: 100000, listC11.size: 100000
EliipsesTime12: 16374561 ns
A12.size: 100000, B12.size: 100000, listC12.size: 100000

Process finished with exit code 0

Conclose

  • Vanila Collection for + if, for + switch case 가 빠르다.
반응형

'lang > java' 카테고리의 다른 글

Java - 2 ArrayList join LinkedHapMap Filter to CSV String  (0) 2022.02.23
java filter speed Test  (0) 2021.12.31
온라인에서 자바를 배울수 있는 사이트 10가지  (0) 2014.10.06
Spring  (0) 2013.07.17
자바 데이터 타입  (0) 2013.07.08
우분투 java  (0) 2013.07.01