Stream Performance Comparison test

Source: Internet
Author: User
Tags int size
Test environment: JDK1.8.0_60, Guava-18.0.jar

Tomap
    public static void Demo1 () {int size = 50000;
        list<string> list = new arraylist<> (size);
        for (int i = 0; i < size; i++) {List.add (integer.tostring (i));
        } System.out.println ("size =" + size);
        Long Start2 = System.currenttimemillis ();
Map<integer, string> map3 = maps.newhashmapwithexpectedsize (size); for (int i = 0; i < size; i++) {//String string = List.get (i);//Map3.put (String.has
Hcode (), string);
        for (string string:list) {Map3.put (String.hashcode (), string);
        Long End2 = System.currenttimemillis ();

        System.out.println ("iterator---t =" + (END2-START2));
        Long start = System.currenttimemillis (); Map<integer, string> map = List.parallelstream (). Collect (Collectors.tomap (S-> s.hashcode (), S-
        > s));
   Long end = System.currenttimemillis ();     SYSTEM.OUT.PRINTLN ("Stream---t =" + (End-start));
        Long Start1 = System.currenttimemillis (); Map<integer, string> map1 = Maps.uniqueindex (list,//lists for value list new Com.google.common.base.Functi On<string, integer> () {@Override public Integer apply (String value) {//return value is
                    Key, the parameter is Value return Value.hashcode ();
        }
                });
        Long end1 = System.currenttimemillis ();
    SYSTEM.OUT.PRINTLN ("Guava---t =" + (END1-START1)); }
Note the map type generated by guava.
Test results:
Size = 5000
iterator---t =
---t =
guava---t =

size = 500000
iterator---t = 148< C5/>stream---t = 1533
Guava---t = 104

Filter
    public static void Demo2 () {int size = 5000;
        list<string> list = new arraylist<> (size);
        for (int i = 0; i < size; i++) {List.add (integer.tostring (i));
        } System.out.println ("Szie =" + size);
        Long Start2 = System.currenttimemillis ();
        List<string> List2 = lists.newarraylistwithcapacity (size);
            for (int i = 0; i < size; i++) {String string = List.get (i);
            if (integer.valueof (string)% 2 = 0) {list2.add (string);
        } Long End2 = System.currenttimemillis ();

        System.out.println ("iterator---t =" + (END2-START2));
        Long start = System.currenttimemillis (); List<string> List1 = List.parallelstream (). Filter (S-> integer.valueof (s)% 2 = 0). Collect (Coll
        Ectors.tolist ());
        Long end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Stream---t =" + (end-sTart)); }
Test results:
Szie = 5000
iterator---t =
---t =

Szie = 500000
iterator---t =
stream---t = 1 33

AnyMatch
    public static void Demo3 () {
        int size = 500000;
        list<string> list = new arraylist<> (size);
        for (int i = 0; i < size; i++) {
            List.add (integer.tostring (i));
        }

        System.out.println ("Szie =" + size);
        Long Start2 = System.currenttimemillis ();
        for (int i = 0; i < size; i++) {
            string string = List.get (i);
            if (String.endswith ("412347")) {break
                ;
            }
        }

        Long End2 = System.currenttimemillis ();
        System.out.println ("iterator---t =" + (END2-START2));

        Long start = System.currenttimemillis ();
        List.parallelstream (). AnyMatch (S-> s.endswith ("1234567"));
        Long end = System.currenttimemillis ();
        SYSTEM.OUT.PRINTLN ("Stream---t =" + (End-start));
    }
Test results:
Szie = 500000
iterator---t =
stream---t = 114






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.