Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Better performance between operations on char arrays and strings

 
rachana ravali
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there any performance advantage if we perform the operations on String by converting them into char arrays ?
 
Campbell Ritchie
Marshal
Posts: 52625
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it and see. There is a method in the System class which enables you to get time from the computer's clock, so you can call that before and after an operation and see what happens. Don't use the millisecond method.

And if you really are on about performance, I think this thread ought to go to the performance forum. Speed and Efficiency, it says there!
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also take a look at the source code for the String class. Internally it uses char array.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are doing multiple single character changes, working with the char array will probably be faster. Substitutions that change the length of the String - you would end up duplicating functions of the String class.

So - the question is, what kind of operations?

Bill
 
Sapika Am
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For most cases String class itself is good . API has enough right use is the trick

Unless using String class operations itself is source of performance bottleneck, try StringBuilder/StringBuffer. if thats not good/ possible yes go for char Arrays.

Its a question of drawing line b/w Readable code, right maintainable implementation and performance. U will have rework for sure in many cases.....

Example cases of apt usages....
CASE : replacing chars in string
EX - s.replace('<','['); Its apt... Its best than char operation
doing s.replace("<","[") --- incorrect use, overkill

CASE: using String.replace("\n","") is slower.
Even for removing invalidXML Chars from external input....
converting to char array and doing array operation, atleast 3X faster

CASE : If doing pattern find and replace then well String.replaceAll is better bet...
Using replaceAll for non patterns will not help performance

CASE: char Access -- doing charAt() v/s (toCharArray() then charArray[i])
cahrArray[n] emerges clear winner at 100.
charAt() : String length (4-10) way faster
charArray[i]: String length 1000+ approaches 0.4X fastness
if source String is constant then changing to charArray is works

I recollect these from my own experience
Let me know more if you know
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!