package sz.fptest;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class fptestMain {
    private static List<String> makeList()
    {
        List<String> result = new ArrayList<>();
        for( int i=0; i<10000; i++)
        {
            result.add( Integer.toString(i) );
        }
        return result;
    }

    private static String tfuntional(List<String> list)
    {
        String result = list
                .stream()
                .collect(Collectors.joining(" "));
        return result+" ";
    }

    private static String tfuntionalPerverted(List<String> list)
    {
        StringBuilder sb = new StringBuilder(20000);
        String result = list
                .stream()
                .reduce("", (acc, name) -> { sb.append(name).append(' '); return ""; } );
        return sb.toString();
    }

    private static String tnormal(List<String> list)
    {
        String result="";
        for( String s : list)
        {
            result += s+' ';
        }
        return result;
    }

    private static String tnormalPrealloc(List<String> list)
    {
        StringBuilder sb = new StringBuilder(20000);
        for( String s : list)
        {
            sb.append(s).append(' ');
        }
        return sb.toString();
    }

    public static void main(String[] args)
    {
        List<String> list = makeList();
        for( int round=1;round<=100;round++) {
            long t1 = System.currentTimeMillis();
            String sf = tfuntional(list);
            long t2 = System.currentTimeMillis();
            String sn = tnormal(list);
            long t3 = System.currentTimeMillis();
            String snp = tnormalPrealloc(list);
            long t4 = System.currentTimeMillis();
            String sfp = tfuntionalPerverted(list);
            long t5 = System.currentTimeMillis();
            System.out.println("tfunctional:" + (t2 - t1) + ", tnormal:" + (t3 - t2) + ", tnormalPrealloc:" + (t4 - t3)+ ", tfunctionalPerverted:" + (t5 - t4));
            if (!sn.equals(sf)) {
                System.out.println("sn != sf");
            }
            if (!snp.equals(sf)) {
                System.out.println("snp != sf");
            }
            if (!sfp.equals(sf)) {
                System.out.println("sfp != sf");
            }
        }
    }
}

@@ -893,22 +893,298 @@
 String t
-normal
+funtionalWasteLotsOfObjects(List%3CString%3E list) %7B%0A        String result = list%0A                .stream()%0A                .map(name -%3E name + %22 %22)%0A                .reduce(%22%22, (acc, name) -%3E acc + name);%0A        return result;%0A    %7D%0A%0A    private static String tnormalWasteLotsOfObjects
 (List%3CSt
@@ -1702,19 +1702,18 @@
 ;round%3C=
-100
+25
 ;round++
@@ -1889,16 +1889,34 @@
  tnormal
+WasteLotsOfObjects
 (list);%0A
@@ -2157,32 +2157,145 @@
 ntTimeMillis();%0A
+            String sfwlo = tfuntionalWasteLotsOfObjects(list);%0A            long t6 = System.currentTimeMillis();%0A
             Syst
@@ -2348,16 +2348,34 @@
  tnormal
+WasteLotsOfObjects
 :%22 + (t3
@@ -2415,16 +2415,37 @@
 t4 - t3)
+%0A                    
 + %22, tfu
@@ -2475,16 +2475,64 @@
 t5 - t4)
+ + %22, tfuntionalWasteLotsOfObjects:%22 + (t6 - t5)
 );%0A     
@@ -2747,32 +2747,32 @@
 p.equals(sf)) %7B%0A
-
                 
@@ -2810,32 +2810,134 @@
 ;%0A            %7D%0A
+            if (!sfwlo.equals(sf)) %7B%0A                System.out.println(%22sfwlo != sf%22);%0A            %7D%0A
         %7D%0A    %7D%0A

add back wasteful functional variant for comparison