GitHub user otaviojava opened a pull request:

    https://github.com/apache/tomee/pull/219

    Avoid String in a loop

    The reason to prefer StringBuilder is that both + and concat create a new 
object every time you call them (provided the right-hand side argument is not 
empty). This can quickly add up to a lot of objects, almost all of which are 
completely unnecessary.
    
    ```java
    public class Main
    {
        public static void main(String[] args)
        {
            long now = System.currentTimeMillis();
            slow();
            System.out.println("slow elapsed " + (System.currentTimeMillis() - 
now) + " ms");
    
            now = System.currentTimeMillis();
            fast();
            System.out.println("fast elapsed " + (System.currentTimeMillis() - 
now) + " ms");
        }
    
        private static void fast()
        {
            StringBuilder s = new StringBuilder();
            for(int i=0;i<100000;i++)
                s.append("*");      
        }
    
        private static void slow()
        {
            String s = "";
            for(int i=0;i<100000;i++)
                s+="*";
        }
    }
    ```
    
    * slow elapsed 11741 ms
    * fast elapsed 7 ms
    
    Also, this PR avoids unnecessary call in StringBuilder

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/otaviojava/tomee 
use_string_builder_instad_string_loop

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/tomee/pull/219.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #219
    
----
commit 442a81b3e78b7700f0420573927872bedbec30a4
Author: Otavio Santana <otaviopolianasantana@...>
Date:   2018-11-22T15:23:49Z

    removes String concatenation loop in Assembler

commit afec54d2204e6e5b8814cddc7eef427a35b54dd5
Author: Otavio Santana <otaviopolianasantana@...>
Date:   2018-11-22T15:24:04Z

    removes String concatenation loop in AnnotationDeployer

commit 20b08f5093e2c562886d6221e5f2a6322bd158e1
Author: Otavio Santana <otaviopolianasantana@...>
Date:   2018-11-22T15:24:17Z

    removes String concatenation loop in SunConvertion

commit e859469a996d4581a971869f685dea5bf9b06b8d
Author: Otavio Santana <otaviopolianasantana@...>
Date:   2018-11-22T15:24:41Z

    removes String concatenation loop in LoggingPreparedSqlStatement

commit 806df6dc4095d0d444357b242560d182544fa69f
Author: Otavio Santana <otaviopolianasantana@...>
Date:   2018-11-22T18:26:57Z

    removes redudant toString

----


---

Reply via email to