• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

time difference in various string concat methods  RSS feed

 
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a program wherein I run a loop a given number of times (iterationCount variable) concatenating a string to another string using 4 techniques: +, concat, StringBuffer and StringBuilder, and print out the time on screen for each of the 4 methods. Each method is allocated a different string to concatenate so as to avoid the constant pool interference between methods. Now

1. for iterationCount = 5000, the StringBuilder method's time is always 0ms. StringBuffer method's time is sometimes 0, sometimes ~16 ms.
2. for iterationCount = 50000, the StringBuffer method's time is always 0ms. StringBuilder method's time is sometimes 0, sometimes ~16 ms.

Q1. Why should one method in either case yield 0 always(5000→StringBuilder , 50000→StringBuffer )?
Q2. Why, upon changing the iterationCount from 5000 to 50000 or vice-versa, the 0 ms elapse time method is changed?

The output on my "intel Core i5 XP" machine is as follows:

Output

47ms over 5000 iterations for "+"
15ms over 5000 iterations for String concat method
16ms over 5000 iterations for StringBuffer append method
0ms over 5000 iterations for StringBuilder append method

47ms over 5000 iterations for "+"
31ms over 5000 iterations for String concat method
0ms over 5000 iterations for StringBuffer append method
0ms over 5000 iterations for StringBuilder append method

47ms over 5000 iterations for "+"
16ms over 5000 iterations for String concat method
0ms over 5000 iterations for StringBuffer append method
0ms over 5000 iterations for StringBuilder append method

47ms over 5000 iterations for "+"
15ms over 5000 iterations for String concat method
16ms over 5000 iterations for StringBuffer append method
0ms over 5000 iterations for StringBuilder append method


7515ms over 50000 iterations for "+"
2938ms over 50000 iterations for String concat method
0ms over 50000 iterations for StringBuffer append method
0ms over 50000 iterations for StringBuilder append method

7640ms over 50000 iterations for "+"
2985ms over 50000 iterations for String concat method
0ms over 50000 iterations for StringBuffer append method
0ms over 50000 iterations for StringBuilder append method

7547ms over 50000 iterations for "+"
2969ms over 50000 iterations for String concat method
0ms over 50000 iterations for StringBuffer append method
15ms over 50000 iterations for StringBuilder append method

7765ms over 50000 iterations for "+"
3000ms over 50000 iterations for String concat method
0ms over 50000 iterations for StringBuffer append method
16ms over 50000 iterations for StringBuilder append method

 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you calculating run time ? I presume using System.currentTimeMillis ?
Use System.nanoTime
<edit>
the key point is that, if you use + or concat then each loop one new object is created (StringBuilder) . hence bit time required.
</edit>
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying Seetharaman. I understand that. My doubts are earmarked as Q1 and Q2.
Edit: With nanoTime(), I could get the time properly and see the time difference. It was < 0 ms for some which are posted as 0 here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!