Java Object creation vs. String parsing
I'm working on migrating a project which is very JSP-centric to using Velocity. In many places the JSP pages were simply parsing strings to display various things. This makes the JSP very ugly and difficult to maintain obviously.
I have modified the Controller for this class to do this work for me (i.e. creating开发者_C百科 a List of POJOs), then iterating over the pieces using velocity. I have a feeling that it's going to come with heavy resistance.
I realize that creating the Object comes with overhead, but it makes our pages much easier to debug, write and use. Besides the fact it separates the UI from the core logic of what is occurring in the background. Not to mention our appservers are BORED. The database is wincing in pain. We are to see a tenfold increase in users (which could be why it was initially developed to parse the strings in the JSP & skip the object creation) -- this smells to me of premature optimization.
What are further arguments to support my claim that we should eat the cost associated with the object creation?
- Object creation in Java was somewhat expensive, about 15 years ago. Java runtimes have a improved a lot since then. Nowadays, Java object creation is often faster than in C++.
- It was never so expensive that it would be a decisive factor in basic application design.
- Doing things in a JSP does not mean no (or even fewer) objects are created. Quite the opposite, most likely. JSPs are compiled to perfectly normal Java code that creates lots of objects.
- It sounds like your company needs to hire at least one person who actually understands how Java works ASAP.
Object creation is cheap. Very cheap. And modern computers are fast. Very fast.
Do some simple profiling. See how long it takes to create a million of your POJOs. It'll be quite quick.
Maintenance - most dollars are spent maintaining software vs. developing. If you make something easy to maintain and extend, it's a blessing.
It's generally frowned upon in this community to do any premature optimization. What kind of hardware are you running on? I imagine servers can handle your workload... I tend to like the idea of making it work well first, and then finding bottle necks. You might find that object creation isn't the biggest problem when you profile it.
精彩评论