Which is more efficient?

M

metfan

Hi,
For the following to snips, which one do you think is more efficient?

snip1:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abc" + "def");

snip2:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abc");
bw.write("def");
 
B

Bjoern

metfan said:
Hi,
For the following to snips, which one do you think is more efficient?

snip1:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abc" + "def");

I think the compiler probably compiles this to bw.write("abcdef"), which
would be faster, but in general if you had bw.write(a+b) with a and b
String variable, then there would actually be a StringBuffer
instanstiated just for joining a and b. So that would definitely be
slower than just writing a and then b to the Buffer.
snip2:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abc");
bw.write("def");



--


Let's not weep for their evil deeds,
but for their lack of imagination
(Nick Cave)
 
J

Jon Skeet

metfan said:
For the following to snips, which one do you think is more efficient?

snip1:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abc" + "def");

snip2:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abc");
bw.write("def");

The first, as it compiles to the same as:

BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abcdef");

because literal strings are concatenated at compile time.
 
M

metfan

For the following to snips, which one do you think is more efficient?
The first, as it compiles to the same as:

BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abcdef");

because literal strings are concatenated at compile time.

Well.. what if:

snip1:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
String a = new String("abc");
String b = new String("def");
bw.write(a + b);

snip2:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
String a = new String("abc");
String b = new String("def");
bw.write(a);
bw.write(b);
 
S

Simon Righarts

metfan said:
Well.. what if:

snip1:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
String a = new String("abc");
String b = new String("def");
bw.write(a + b);

snip2:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
String a = new String("abc");
String b = new String("def");
bw.write(a);
bw.write(b);

You won't notice any real difference, but #1 would be faster, since String
concatentation is a lot faster thn an extra I/O call.
 
M

Michael Borgwardt

metfan said:
Well.. what if:

snip1:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
String a = new String("abc");
String b = new String("def");
bw.write(a + b);

snip2:
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
String a = new String("abc");
String b = new String("def");
bw.write(a);
bw.write(b);

In that case, the second one is faster, for the reason Bjoern gave. But
you probably shouldn't worry.
 
N

Nils O. =?iso-8859-1?Q?Sel=E5sdal?=

The first, as it compiles to the same as:

BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(System.out));
bw.write("abcdef");

because literal strings are concatenated at compile time.
Essentially, just compile the 2 codes snippets, and
use jad to decompile it. You would see what happens..
 
M

Michael Borgwardt

Simon said:
You won't notice any real difference, but #1 would be faster, since String
concatentation is a lot faster thn an extra I/O call.

There is no extra IO call because it's a BufferedWriter.
 
R

Roedy Green

String a = new String("abc");
String b = new String("def");

that is just stuttering that creates new String objects to no purpose.

The net effect is the same as

String a = "abc";
String b = "def";

see http://mindprod.com/jgloss/newbie.html

A smart compiler could do the concatenation at compile time, though it
would not be guaranteed the way "abc" + "def" is.
 
R

Roedy Green

You won't notice any real difference, but #1 would be faster, since String
concatentation is a lot faster thn an extra I/O call.

Not necessarily. The string i/o could be just an arraycopy with a
buffered file. The concatenation allocates a String buffer, copies the
two strings, and then converts the StringBuffer into a String.
 
B

brougham5

metfan said:
Well.. what if:

javap is your friend. Look at the bytecode and see what the difference is.

Create a test harness that will measure the differences for you, as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,085
Members
48,773
Latest member
Kaybee

Latest Threads

Top