Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Which is more efficient?

Reply
Thread Tools

Which is more efficient?

 
 
metfan
Guest
Posts: n/a
 
      10-24-2003
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");


--
Best regards,
Qingjia Zhu
Q@z@J
 
Reply With Quote
 
 
 
 
Bjoern
Guest
Posts: n/a
 
      10-24-2003
metfan wrote:
> 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)

 
Reply With Quote
 
 
 
 
Jon Skeet
Guest
Posts: n/a
 
      10-24-2003
metfan <qjzhupublic@___NoSpam__yahoo.ie> wrote:
> 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.

--
Jon Skeet - <>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
metfan
Guest
Posts: n/a
 
      10-24-2003
>> 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.
>


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);


--
Best regards,
Qingjia Zhu
Q@z@J
 
Reply With Quote
 
Simon Righarts
Guest
Posts: n/a
 
      10-24-2003

"metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
news...
> 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.

>
> --
> Best regards,
> Qingjia Zhu
> Q@z@J



 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-24-2003
metfan wrote:
> 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.

 
Reply With Quote
 
Nils O. =?iso-8859-1?Q?Sel=E5sdal?=
Guest
Posts: n/a
 
      10-24-2003
In article <MPG.1a02e94f7f5c63fc9896c1@10.1.1.14>, Jon Skeet wrote:
> metfan <qjzhupublic@___NoSpam__yahoo.ie> wrote:
>> 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.

Essentially, just compile the 2 codes snippets, and
use jad to decompile it. You would see what happens..

 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-24-2003
Simon Righarts wrote:

> 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.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-24-2003
On Fri, 24 Oct 2003 16:21:25 +0800, metfan
<qjzhupublic@___NoSpam__yahoo.ie> wrote or quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-24-2003
On Fri, 24 Oct 2003 22:14:05 +1300, "Simon Righarts"
<> wrote or quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
ADSL WIC support - which NM's, and which IOS versions? Kralizec Craig Cisco 5 12-08-2005 02:20 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
Keeping track of which user controls need to be loaded and which not John ASP .Net 0 07-08-2003 09:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57