You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by otaviojava <gi...@git.apache.org> on 2018/11/26 13:44:05 UTC
[GitHub] tomee pull request #219: Avoid String in a loop
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 <ot...@...>
Date: 2018-11-22T15:23:49Z
removes String concatenation loop in Assembler
commit afec54d2204e6e5b8814cddc7eef427a35b54dd5
Author: Otavio Santana <ot...@...>
Date: 2018-11-22T15:24:04Z
removes String concatenation loop in AnnotationDeployer
commit 20b08f5093e2c562886d6221e5f2a6322bd158e1
Author: Otavio Santana <ot...@...>
Date: 2018-11-22T15:24:17Z
removes String concatenation loop in SunConvertion
commit e859469a996d4581a971869f685dea5bf9b06b8d
Author: Otavio Santana <ot...@...>
Date: 2018-11-22T15:24:41Z
removes String concatenation loop in LoggingPreparedSqlStatement
commit 806df6dc4095d0d444357b242560d182544fa69f
Author: Otavio Santana <ot...@...>
Date: 2018-11-22T18:26:57Z
removes redudant toString
----
---
[GitHub] tomee pull request #219: Avoid String in a loop
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/tomee/pull/219
---
[GitHub] tomee pull request #219: Avoid String in a loop
Posted by danielsoro <gi...@git.apache.org>.
Github user danielsoro commented on a diff in the pull request:
https://github.com/apache/tomee/pull/219#discussion_r236279011
--- Diff: container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java ---
@@ -2939,12 +2939,14 @@ public EjbModule deploy(final EjbModule ejbModule) throws OpenEJBException {
}
if (interfaces.size() != 1) {
- String msg = "When annotating a bean class as @MessageDriven without declaring messageListenerInterface, the bean must implement exactly one interface, no more and no less. beanClass=" + clazz.getName() + " interfaces=";
+ StringBuilder msg = new StringBuilder("When annotating a bean class as @MessageDriven without" +
--- End diff --
What do you think to use String.format in cases like that?
---