Which is more efficient?

Discussion in 'Java' started by metfan, Oct 24, 2003.

  1. metfan

    metfan Guest

    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
     
    metfan, Oct 24, 2003
    #1
    1. Advertising

  2. metfan

    Bjoern Guest

    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)
     
    Bjoern, Oct 24, 2003
    #2
    1. Advertising

  3. metfan

    Jon Skeet Guest

    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
     
    Jon Skeet, Oct 24, 2003
    #3
  4. metfan

    metfan Guest

    >> 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
     
    metfan, Oct 24, 2003
    #4
  5. "metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
    news:eek:...
    > 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
     
    Simon Righarts, Oct 24, 2003
    #5
  6. 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.
     
    Michael Borgwardt, Oct 24, 2003
    #6
  7. 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..
     
    Nils O. =?iso-8859-1?Q?Sel=E5sdal?=, Oct 24, 2003
    #7
  8. 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.
     
    Michael Borgwardt, Oct 24, 2003
    #8
  9. metfan

    Roedy Green Guest

    string literal contatenation

    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.
     
    Roedy Green, Oct 24, 2003
    #9
  10. metfan

    Roedy Green Guest

    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.
     
    Roedy Green, Oct 24, 2003
    #10
  11. metfan

    Guest

    metfan <qjzhupublic@___NoSpam__yahoo.ie> wrote:

    > 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.
     
    , Oct 25, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant Merwitz

    sessions, which is the more efficient way

    Grant Merwitz, May 11, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    390
    Grant Merwitz
    May 12, 2005
  2. kaeli
    Replies:
    8
    Views:
    593
    Chris Smith
    Nov 18, 2004
  3. CodeCracker
    Replies:
    25
    Views:
    3,801
    Richard Herring
    May 23, 2005
  4. Dustan

    Which is More Efficient?

    Dustan, May 18, 2006, in forum: Python
    Replies:
    16
    Views:
    437
    John Machin
    May 19, 2006
  5. Replies:
    15
    Views:
    486
    Chris Uppal
    Oct 14, 2006
Loading...

Share This Page