Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] stringbuffer optimization

Started by sma202 Jan 23, 2001 at 2:05 PM 0 replies 450+ views
Original Post
sma202
sma202
Heres something I was wondering about, suppose we have the following statement: StringBuffer sb = new StringBuffer() sb.append(a); sb.append(b); sb.append(c); And contrast it with this code: StringBuffer sb = new StringBuffer() synchronized (sb) { sb.append(a); sb.append(b); sb.append(c); } Is the second piece of code faster because the first code has to perform 3 internal locks within the stringbuffer class versus only one in second piece of code?
GKW
GKW
I would be interested in the answer to this. But unless you need the string concat "+" operation I would suggest making your own string class that does not check for sync, assuming you don''t need it. I assume that even if it has a lock from the start, the stringbuffer will still check for a lock.

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.