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/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=41779:/home/username/.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/username/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/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=33691:/home/username/.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/username/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/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=40327:/home/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/username/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/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=42073:/home/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/username/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/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=43265:/home/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/username/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/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/lib/idea_rt.jar=44303:/home/username/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/213.6461.79/bin -Dfile.encoding=UTF-8 -classpath /home/username/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 가 빠르다?
20230608
package org.example;
import javax.swing.text.NumberFormatter;
import java.text.NumberFormat;
import java.util.*;
import java.util.stream.Collectors;
public class arrayListFilterSpeedTest {
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("\nEliipsesTime(for(item: items) if else: " + NumberFormat.getNumberInstance(Locale.US).format(System.nanoTime() - begin) + " ns");
System.out.println("A1.size: " + listA1.size() + ", B1.size: " + listB1.size() + ", listC1.size: " + listC1.size());
List<Student> listA1_0 = new ArrayList<>(),
listB1_0 = new ArrayList<>(),
listC1_0 = new ArrayList<>();
long begin_0 = System.nanoTime();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
String name = student.getName();
if (name.equals("A")) listA1_0.add(student);
else if (name.equals("B")) listB1_0.add(student);
else if (name.equals("C")) listC1_0.add(student);
}
System.out.println("\nEliipsesTime(for(int i = 0) if else: " + NumberFormat.getNumberInstance(Locale.US).format(System.nanoTime() - begin_0) + " ns");
System.out.println("A1_0.size: " + listA1_0.size() + ", B1_0.size: " + listB1_0.size() + ", listC1_0.size: " + listC1_0.size());
List<Student> listA1_1 = new ArrayList<>(),
listB1_1 = new ArrayList<>(),
listC1_1 = new ArrayList<>();
long begin_1 = System.nanoTime();
students.forEach(student -> {
String name = student.getName();
if (name.equals("A")) listA1_1.add(student);
else if (name.equals("B")) listB1_1.add(student);
else if (name.equals("C")) listC1_1.add(student);
});
System.out.println("\nEliipsesTime(forEach(item) if else): " + NumberFormat.getNumberInstance(Locale.US).format(System.nanoTime() - begin_1) + " ns");
System.out.println("A1.size: " + listA1_1.size() + ", B1.size: " + listB1_1.size() + ", listC1.size: " + listC1_1.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("\nEliipsesTime(for(item: items) switch): " + NumberFormat.getNumberInstance(Locale.US).format(System.nanoTime() - begin2) + " ns");
System.out.println("A2.size: " + listA2.size() + ", B2.size: " + listB2.size() + ", listC2.size: " + listC2.size());
List<Student> listA2_0 = new ArrayList<>(),
listB2_0 = new ArrayList<>(),
listC2_0 = new ArrayList<>();
long begin2_0 = System.nanoTime();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
switch (student.getName()) {
case "A": listA2_0.add(student);break;
case "B": listB2_0.add(student);break;
case "C": listC2_0.add(student);break;
default: break;
}
}
System.out.println("\nEliipsesTime(for(int i = 0) switch): " + NumberFormat.getNumberInstance(Locale.US).format(System.nanoTime() - begin2_0) + " ns");
System.out.println("A2_0.size: " + listA2_0.size() + ", B2_0.size: " + listB2_0.size() + ", listC2_0.size: " + listC2_0.size());
List<Student> listA2_1 = new ArrayList<>(),
listB2_1 = new ArrayList<>(),
listC2_1 = new ArrayList<>();
long begin2_1 = System.nanoTime();
students.forEach(student -> {
String name = student.getName();
switch (student.getName()) {
case "A": listA2_0.add(student);break;
case "B": listB2_0.add(student);break;
case "C": listC2_0.add(student);break;
default: break;
}
});
System.out.println("\nEliipsesTime(forEach switch): " + NumberFormat.getNumberInstance(Locale.US).format(System.nanoTime() - begin2_1) + " ns");
System.out.println("A2_1.size: " + listA2_1.size() + ", B2_1.size: " + listB2_1.size() + ", listC2_1.size: " + listC2_1.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("\nEliipsesTime(stream().filter) " + NumberFormat.getNumberInstance(Locale.US).format(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("\nEliipsesTime(farallelStream().filter): " + NumberFormat.getNumberInstance(Locale.US).format(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;
}
}
sutudents size: 300000
EliipsesTime(for(item: items) if else: 68,660,800 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime(for(int i = 0) if else: 51,012,300 ns
A1_0.size: 100000, B1_0.size: 100000, listC1_0.size: 100000
EliipsesTime(forEach(item) if else): 149,298,500 ns
A1.size: 100000, B1.size: 100000, listC1.size: 100000
EliipsesTime(for(item: items) switch): 48,158,600 ns
A2.size: 100000, B2.size: 100000, listC2.size: 100000
EliipsesTime(for(int i = 0) switch): 27,799,200 ns
A2_0.size: 100000, B2_0.size: 100000, listC2_0.size: 100000
EliipsesTime(forEach switch): 29,306,300 ns
A2_1.size: 0, B2_1.size: 0, listC2_1.size: 0
EliipsesTime(stream().filter) 85,269,600 ns
A11.size: 100000, B11.size: 100000, listC11.size: 100000
EliipsesTime(farallelStream().filter): 48,430,100 ns
A12.size: 100000, B12.size: 100000, listC12.size: 100000
반응형