Last active
December 7, 2018 08:54
-
-
Save RareScrap/527b3bc531811600dd7bd65a44e62cd1 to your computer and use it in GitHub Desktop.
Замер производительности при удалении элементов из ArrayList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javaapplication16; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import javafx.print.Collation; | |
/** | |
* | |
* @author rares | |
*/ | |
public class JavaApplication16 extends javax.swing.JFrame { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
ArrayList<Object> l; | |
long time1, time2; | |
// Удаление перебором | |
l = init(); | |
time1 = System.currentTimeMillis(); | |
for (int i = l.size()-1; i >= 0; i--) { // На малых массивах (~100_000) показывается лучший результат. На больших массивах (~50_000_000) уступает способу №3 | |
l.remove(i); | |
} | |
time2 = System.currentTimeMillis(); | |
System.err.println("1: Разница " + (time2 - time1)); | |
// Более компактное удаление перебором | |
l = init(); | |
time1 = System.currentTimeMillis(); | |
//while (l.remove(null)); // Самый медленный способ. Полный пиздец. Боюсь запускать его более чем с 10к итемами | |
time2 = System.currentTimeMillis(); | |
System.err.println("2: Разница " + (time2 - time1)); | |
l = init(); | |
time1 = System.currentTimeMillis(); | |
l.removeAll(Collections.singleton(null)); // Полная противоположность способу №1 | |
time2 = System.currentTimeMillis(); | |
System.err.println("3: Разница " + (time2 - time1)); | |
l = init(); | |
time1 = System.currentTimeMillis(); | |
// for (Iterator<Object> itr = l.iterator(); itr.hasNext();) { | |
// if (itr.next() == null) { itr.remove(); } // Почти так же медленно как №3 | |
// } | |
time2 = System.currentTimeMillis(); | |
System.err.println("4: Разница " + (time2 - time1)); | |
l = init(); | |
time1 = System.currentTimeMillis(); | |
List s1=new ArrayList(); | |
s1.add(null); | |
l.removeAll(s1); | |
time2 = System.currentTimeMillis(); | |
System.err.println("5: Разница " + (time2 - time1)); | |
} | |
public static ArrayList<Object> init() { | |
Object[] objects = new Object[100_000]; // Больше я не поставлю, иначе способ 2 придется дожилаться тыщу лет | |
return new ArrayList<Object>( Arrays.asList(objects) ); | |
} | |
} | |
/* /////////////////////////////////////////////////////////////////////////////////// | |
На 100к итемов: | |
1: Разница 7 | |
2: Разница 1690 | |
3: Разница 17 | |
4: Разница 1837 | |
5: Разница 12 | |
На 50млн итемов: | |
1: Разница 156 | |
2: Разница Я НЕ БУДУ СТОЛЬКО ЖДАТЬ | |
3: Разница 149 | |
4: Разница Я НЕ БУДУ СТОЛЬКО ЖДАТЬ | |
5: Разница 283 | |
//////////////////////////////////////////////////////////////////////////////////// */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment