Java7ã§List/TreeMap/TreeSetã®Iteratorãããªãæ©ããªã£ã¦ã
コレクションの細かい話、だが面白い! - 谷本 心 in せろ部屋
ArrayListã®foreachæï¼æ¡å¼µforæï¼ããæ®éã®foræããæ©ããªãã¾ããã
ãããªãã§ããï¼ï¼
â¦ã¨æã£ã¦è©¦ãã¦ã¿ãã¨ãããArrayList ã® foreachæï¼æ¡å¼µforæï¼ã Java6 â Java7 ã§ç´2.5åéããªã£ã¦ãã¾ããã
æå
ã®ç°å¢ã§ã¯ãæ®éã® for æããéããªããã¨ã¯ãªãã£ããã®ã®ãã»ã¨ãã©æ°ã«ãªãªããªããããã®å·®ã§ããã
-
-
- -
-
2011å¹´11æ26æ¥ è¨æ£ï¼
éããªã£ã¦ããã®ã¯ ClientVM ã§å®è¡ããå ´åã®ã¿ã§ãServerVM ã§ã¯å¤ãã£ã¦ãã¾ããã§ããã
詳ããã¯ãJavaの実行速度を調べるなら、ClientVM/SeverVM の違いを考慮しておくべきでした。 - 地平線に行く ãã覧ãã ããã
-
-
- -
-
éããªã£ãçç±
ä½ãå¤ãã£ããã ããï¼ ã¨ãããã¨ã§ãAPIã®ã½ã¼ã¹ã³ã¼ãã確èªãã¦ã¿ãã¨ãããJava7 㧠ArrayList ã® Iterator ããªãã¡ã¯ã¿ãªã³ã°ããã¦ãã¾ããã
Java6 ã®å ´åãArrayList 㯠Iterator ãå®è£
ãããAbstractList ã®æ±ç¨çãªå®è£
ã使ç¨ãã¦ãã¾ããã
ãã® AbstractList ã®å®è£
ã¯ã åè¦ç´ ã get(int) ã§åå¾ãã¦ããã®æã«çºçããä¾å¤ã try-catch ã§æã¾ãã¦ãIterator ç¨ã®ä¾å¤ã«å¤æããããã«ãªã£ã¦ãã¾ããã
// Java6, AbstractList - 342è¡ç®ã public E next() { checkForComodification(); try { E next = get(cursor); lastRet = cursor++; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } }
ä¸æ¹ãJava7 ã§ã¯ãArrayList ã AbstractList ã® Iterator ããªã¼ãã¼ã©ã¤ããã¦ãã¾ããã
ããã¦ããã®ãªã¼ãã¼ã©ã¤ããã Iterator ã§ã¯ãget(int) ã使ç¨ãããç´æ¥ãã§ãã¯ã¨ä¾å¤ã®ã¹ãã¼ãè¡ãããã«ãªã£ã¦ãã¾ããã
// Java7, ArrayList - 790è¡ç®ã @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; }
ãã®å·®ãã¤ã¾ã try-catch ã使ç¨ããªããªã£ããã¨ããã¹ãã¼ãã¢ããã®ç§è¨£ã®ããã§ãã
試ãã«ãJava6 ã®ã½ã¼ã¹ãã try-catch ãæãã¦ã¿ãã¨*1ãforæã§ã¢ã¯ã»ã¹ããå ´åã¨åãé度ã«ãªãã¾ããã
try-catch ã使ã£ãããã¨ãã£ã¦ãã®ãããé
ããªãããã§ã¯ãªãã¨æãã¾ããããã¯ãã«ã¼ãã®ä¸ã§ä½åº¦ãåºããå
¥ã£ãããç¹°ãè¿ãã¨ãããªãã«å½±é¿ãåºã¦ãã¾ãããã§ãã
å®é¨
ArrayList(è¦ç´ 10,000å) à 1,000åã«ã¼ãã§è©¦ãã¦ã¿ã¾ããã*2 *3ã
- foreach ã§ã«ã¼ã
- Java6 : 339ms
- Java7 : 133ms
- for 㨠List.get(i) ã§ã«ã¼ã
- Java6 : 194ms
- Java7 : 116ms
ãããªããforeach(String str : list) ã¨å®å¿ãã¦æ¸ãã¾ãã
ãã¹ãããã°ã©ã
import java.util.ArrayList; import java.util.List; import java.util.Random; import org.junit.BeforeClass; import org.junit.Test; public class SpeedTest { private static List<String> list; @BeforeClass public static void beforeClass(){ Random random = new Random(); list = new ArrayList<String>(); for(int i = 0; i < 10000; i++){ list.add(Integer.toString(random.nextInt())); } } @Test public void listForeachTest(){ long start = System.nanoTime(); for(int i = 0; i < 1000; i++){ for(String str : list){ str.isEmpty(); } } long end = System.nanoTime(); System.out.println("List & foreach : " + ((end - start) / 1000000.0) + "ms"); } @Test public void listForTest(){ long start = System.nanoTime(); for(int i = 0; i < 1000; i++){ for(int j = 0; j < list.size(); j++){ list.get(j).isEmpty(); } } long end = System.nanoTime(); System.out.println("List & for : " + ((end - start) / 1000000.0) + "ms"); } }