• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuilder vs other java Objects

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I did the code using stringbuilders which are not as efficient as using some Java objects on the session and a JSP include to generate the html. I need do it more efficient. Which java objects should I use? Any idea, please?

Regards

Thanks
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angus Ferguson wrote:I did the code using stringbuilders which are not as efficient as using some Java objects


What makes you think that ? Did you profile your application or did you read it somewhere ? If the latter, did they suggest alternatives ?

It all depends on what you are doing with the StringBuilders.
At the moment your question is like asking - "At the moment I'm walking. What can I do that is more efficient ?"
If you're visiting a neighbour, then walking is probably the most efficient. If you want to get to the other side of the world, then taking a plane might be a good idea.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am creating HTML in the java class like



but I want use some Java objects on the session and a JSP include to generate the html in order to improve performance

Thanks
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I believe that building up HTML in strings is a really poor practice -- HTML as template text is why JSPs exist int he first place -- what makes you think the alternative approach will be any more efficient? Are you just guessing?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angus Ferguson wrote:but I want use some Java objects on the session and a JSP include to generate the html in order to improve performance...


Joanne's absolutely right: you've got this bass-ackwards.

1. Does your program work? Don't even think about efficiency until you can say 'yes'.
2. Is your program running too slowly? If not: see 1 (and you'd better have some good metrics to back it up).
3. Do you KNOW that the reason it's running slowly is because of your StringBuilder? If not: again, see 1.

Basic lesson: Don't worry about efficiency until you have a well-designed, and well-written, working solution.

W.A. Wulf wrote:More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason — including blind stupidity.


Winston
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well

I mean don't generate the HTML in the Java, use a JSP to do that based on an object.

What I meant is create an object based on the back end and stick it in the session after that retrieve the object in the session from the .JSP. Is that correct for you?

Use the object from the session and a JSP to generate the html. Then you only create the object once per session and the slow string manipulation is done by tomcat which is optimised for that kind of work

What way you would do that?

Thanks
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angus Ferguson wrote:and the slow string manipulation is done by tomcat which is optimised for that kind of work


Again, you still seem to be assuming that it's the string manipulation that's slow. Do you have any proof?

That said, it's certainly better to only have something done once if you can; and in a client/server environment, that generally means doing it at the server end.

However, we (or at least I) still have no idea what your actual problem is; and in the absence of other information, my first port-of-call would be your network pipeline. You can make all sorts of things server-centric, but if you don't have the network to support it, no amount of Java-based efficient code is going to change things.

Winston
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you should be creating an object/bean in the controller (or a delegate such as the model), and placing it one of request or session scopes in order to generate the HTML in the JSP. Prefer request scope unless you have a really really good reason to put it in session scope.

But you do this because it's the right way to do it; not because it may (or even may not) have some infinitesimal performance gain.

As said before, worry about performance when and if a performance problem raises its head. Always code for clarity; not supposed performance.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic