Efficiency of Java “Double Brace Initialization”?

前端 未结 15 2166
清歌不尽
清歌不尽 2020-11-21 15:35

In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax:

Set flavors = new HashSet         


        
15条回答
  •  Happy的楠姐
    2020-11-21 16:34

    Loading many classes can add some milliseconds to the start. If the startup isn't so critical and you are look at the efficiency of classes after startup there is no difference.

    package vanilla.java.perfeg.doublebracket;
    
    import java.util.*;
    
    /**
     * @author plawrey
     */
    public class DoubleBracketMain {
        public static void main(String... args) {
            final List list1 = new ArrayList() {
                {
                    add("Hello");
                    add("World");
                    add("!!!");
                }
            };
            List list2 = new ArrayList(list1);
            Set set1 = new LinkedHashSet() {
                {
                    addAll(list1);
                }
            };
            Set set2 = new LinkedHashSet();
            set2.addAll(list1);
            Map map1 = new LinkedHashMap() {
                {
                    put(1, "one");
                    put(2, "two");
                    put(3, "three");
                }
            };
            Map map2 = new LinkedHashMap();
            map2.putAll(map1);
    
            for (int i = 0; i < 10; i++) {
                long dbTimes = timeComparison(list1, list1)
                        + timeComparison(set1, set1)
                        + timeComparison(map1.keySet(), map1.keySet())
                        + timeComparison(map1.values(), map1.values());
                long times = timeComparison(list2, list2)
                        + timeComparison(set2, set2)
                        + timeComparison(map2.keySet(), map2.keySet())
                        + timeComparison(map2.values(), map2.values());
                if (i > 0)
                    System.out.printf("double braced collections took %,d ns and plain collections took %,d ns%n", dbTimes, times);
            }
        }
    
        public static long timeComparison(Collection a, Collection b) {
            long start = System.nanoTime();
            int runs = 10000000;
            for (int i = 0; i < runs; i++)
                compareCollections(a, b);
            long rate = (System.nanoTime() - start) / runs;
            return rate;
        }
    
        public static void compareCollections(Collection a, Collection b) {
            if (!a.equals(b) && a.hashCode() != b.hashCode() && !a.toString().equals(b.toString()))
                throw new AssertionError();
        }
    }
    

    prints

    double braced collections took 36 ns and plain collections took 36 ns
    double braced collections took 34 ns and plain collections took 36 ns
    double braced collections took 36 ns and plain collections took 36 ns
    double braced collections took 36 ns and plain collections took 36 ns
    double braced collections took 36 ns and plain collections took 36 ns
    double braced collections took 36 ns and plain collections took 36 ns
    double braced collections took 36 ns and plain collections took 36 ns
    double braced collections took 36 ns and plain collections took 36 ns
    double braced collections took 36 ns and plain collections took 36 ns
    

提交回复
热议问题