package sz.fptest;

import java.util.ArrayList;
import java.util.List;

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()
                .map(name -> name + " ")
                .reduce("", (acc, name) -> acc+name);
        return result;
    }

    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();
            System.out.println("tfunctional:" + (t2 - t1) + ", tnormal:" + (t3 - t2)+ ", tnormalPrealloc:" + (t4 - t3));
            if (!sn.equals(sf)) {
                System.out.println("sn != sf");
            }
            if (!snp.equals(sf)) {
                System.out.println("snp != sf");
            }
        }
    }
}

@@ -556,32 +556,338 @@
  result;%0A    %7D%0A%0A
+    private static String tfuntionalPerverted(List%3CString%3E list)%0A    %7B%0A        StringBuilder sb = new StringBuilder(20000);%0A        String result = list%0A                .stream()%0A                .reduce(%22%22, (acc, name) -%3E sb.append(name).append(' ') !=null ? %22%22 : %22%22);%0A        return sb.toString();%0A    %7D%0A%0A
     private stat
@@ -1170,32 +1170,35 @@
 StringBuilder(20
+-+-
 000);%0A        fo
@@ -1769,32 +1769,134 @@
 ntTimeMillis();%0A
+            String sfp = tfuntionalPerverted(list);%0A            long t5 = System.currentTimeMillis();%0A
             Syst
@@ -1963,16 +1963,17 @@
 t3 - t2)
+ 
 + %22, tno
@@ -1998,16 +1998,55 @@
 t4 - t3)
++ %22, tfunctionalPerverted:%22 + (t5 - t4)
 );%0A     
@@ -2226,32 +2226,130 @@
 ;%0A            %7D%0A
+            if (!sfp.equals(sf)) %7B%0A                System.out.println(%22sfp != sf%22);%0A            %7D%0A
         %7D%0A    %7D%0A

add perverted functional method