What's the difference between calling PrintWritter print () many times, or adding a line and then just printing once? What's better?

In a servlet, usually people will output something like below.

PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
...Many lines here...
out.print("</body>");
out.print("</html>");

      

Write on different lines because it's much easier to read and modify in the future if needed, but I think this is code like the following is valid (generic)?

PrintWriter out = response.getWriter();
String outView = "<html>"
  + "<head>"
  + "</head>"
  + "<body>"
  ...Many lines here...
  + "</body>"
  + "</html>";
out.print(outView);

      

Who cares? And how is the performance of each of them?

+3


source to share


3 answers


Compose your own JSP experience. I know which is out.print()

better than concatenating strings

.

You may have been working with BufferedOutputStream

in the file IO

. When you write your data to a file, (which actually gets written in a BUFFER)

and whenever you think the data should be persistent, you call flush()

. And your data is written to a file. Several times I've been left wondering why my file is empty. Then in the end I discovered that I hadn't called flush()

.

It works the same way out.print()

. out

writes data to a buffer, there are several functions

to control its behavior. So every time you call out.print()

it adds a string to the buffer. And it's fast compared to String concatentation

.

While we are concatenating lines, as lines immutable

, new lines are created. So for this

"<head>" + "</head>" + "<body>"

Total 5

String objects are created.

  • "<head>"

  • "</head>"

  • "<body>"

  • "<head></head>"

  • "<head></head><body>"



And this continues until all your lines are there concatenated

.

But in case out.print()

only 3

String Objects are created .

I worked on JSP, I created Table

in JSP, at my work. Don't blame me , not my fault, this is the way they work here :)

...

I replaced the String padding with StringBuffer

and it sped up the JSP. I used to think it was slow because it accesses data and creates a table at the same time, so it is slow, but there was a problem String addition

. Therefore, from my experience they always go out.print()

.

If you really need to create an output before writing it. Use StringBuffer

, and when writing back, use toString()

to convert it to String. This will save you a lot of time.

String concatenation gives a clear picture several times, use it in development but for production use StringBuffer

. It will be much faster, depending on the number of concatenations you do in your code.

+1


source


This question reminds me of this great blog post on micro-optimization: The Sad Tragedy of Micro-Optimization

Basically, it boils down to the notion that you shouldn't worry about tiny optimization differences like this, but always choose the option that has the best readability. This will maximize your profit in the long run as it will make your code more manageable. It will be easier to maintain, reuse, and improve.



And from this point of view: both methods you mention are pretty awful because they involve printing html directly from servlets. This makes the html difficult to read and debug. You have to maintain html in separate files or use some kind of templating engine like jsp or jsf.

+2


source


Ok, as far as I remember, the JVM has something like a string pool. This means that all lines 1."<head>" 2. "</head>" 3. "<body>"

.. etc. Stored there. This means that if you have something like

String v1="head";
String v2="head";

      

both v1 and v2 will refer to the same object. These objects are eligible for garbage collection if there are no references to them. Now the problem I'm thinking about (I've never tried it) is if your "outView" gets so big that it doesn't fit into the row pool, you get an outofmemeoryexception ... where "response.getWriter" returns an internally buffered object. it uses heap space for internal buffering, so the "response.getWriter" approach is better.

0


source







All Articles