|
|
||
Bernt Johnsen's BlogSeptember 2005 ArchivesComments on "Easy String Concatenation Considered Harmful?"Posted by bernt on September 28, 2005 at 02:15 PM | Permalink | Comments (0)I received some interesting comments on Easy String Concatenation Considered Harmful? and want to comment some of them. I too love the easy string concatenation of Java. But ease of use and the readable code that comes from it will very often hinder the programmer from seeing the problem when it occurs. I also agree that performance optimization should not be a developers first concern. But my article was written from my experience that when programmers encounters performance problems, lack of prepared statements (and their re-use) often is the problem. And using persistence layers isn't always the cure, since I've seen auto-generated code with the same flaw. And then, you can't just get around it easily without either choosing another persistence layer or fixing the the persistence layer.
And, yes, "select * from...." is bad practice and due to my laziness when composing the article.
Easy String Concatenation Considered Harmful?Posted by bernt on September 20, 2005 at 05:33 AM | Permalink | Comments (6)This is not about the amount of String objects generated when you do string concatenation in Java. This is about databases and JDBC. Performance problem?When encountering performance problems programmers have with Derby and other databases I very often find application code like
Statement s = c.createStatement(); while (This will result in a prepare (or compilation as some will call it) of the query each time executeQuery is called, and since the query will be different each time, no statement cache mechanism can help you avoid the compilation, either. Since prepare often is much heavier than execution, this will execute pretty slow. Preformance problem solved!The problem is easily solved with the following pattern (which most programmers should know):
PreparedStatement ps = c.prepareStatement("select * from t where i > ?");
while (
Prepare is done only once, and if the loop iterates one zillion times, this really amounts to something!
Why?There may be several possible explanations to this and one of them could be:It's far too easy to concatenate strings in Java! Programmers are obviously lazy (and need to meet deadlines), and the last (and most efficient) alternative takes somewhat more energy to write. I often find myself in situations where I use the first variant because I'm in a hurry. I'm just writing a code piece for bug-finding, some sample code or I thinking might optimize it later. I'm beginning to wonder if it's a bad habit. If you look at C code and ODBC, it's the other way round. Programmers use the prepared statements and string concatenation is a nuisance with strncat, strdup and strncpy, so in ODBC, the last variant would be the easiest one to write. Also done by toolsI have also found the same pattern in application code generated by some tools. I have even seen this:
while (which for some databases will give you an extra round-trip to the database, too! (Derby may run in embedded mode where the extra procedure call does not matter much). Be prepared!The lesson from this is pretty simple:
Use prepared statements! (and don't forget to reuse them.)
JavaZone, NorwayPosted by bernt on September 17, 2005 at 07:09 AM | Permalink | Comments (0)Since this is my first blog on weblogs.java.net from a small country, Norway, I'll start with a short comment on Norway & Java. Norway has a population of around 5 million people, no large cities, lots of beautiful nature, has become rich from vast oil resources and fisheries, and does not bother to be a part of the EU. Small country? Definitely: Oslo, the capital, has around half a million inhabitants. In the third largest city, Trondheim, where I live, we are around 155000. But compared to many small countries in Europe, Norway is geographically large and sparsely populated (Trondheim is nearly 500 km north of Oslo). Still, each year, the Norwegian Java User Group, JavaBin, hosts a large Java developer conference called JavaZone. JavaZone gathers 1000 attendees and a host of speakers from several countries in 5 parallell tracks. I had the pleasure of speaking at JavaZone this year, and was impressed of both the quality and the diversity of the conference.
Not bad for a small country far north in Euorpe.
| ||
|
|