Optimization 35: Binary I/O Up to this point

Optimization 35: Binary I/O Up to this point we have focused all our attention on the performance aspects of producing and consuming human-readable I/O. Towards that goal we have stripped away all other computations that might have consumed our precious CPU cycles and clouded the I/O performance picture we were trying to highlight. For example, we constructed the String representation of a Trade object right in the Trade constructor so as to take this computation outside the performance-critical path of our specific test program. There are two major drawbacks to that approach. . First, this approach is not very realistic. When we read a Trade String representation from the input file, we never bothered to reconstruct the underlying Trade object. We simply tossed away the line we read not a very realistic solution. . Second, we have committed a performance sin by computing the Trade String representation inside its constructors. This is a very bad idea. An object, in general, may be used in many different ways without ever needing to call its toString() method. If we call such an expensive method in the object constructor, we are penalizing all users of the class, whether they need this computation or not. One of the points we try to stress in this book is that you need to defer costly computations to the point at which you actually need them (Optimization 12). We have violated this principle in order to drive the I/O performance story. So let’s put the discussion back on more realistic grounds. We will modify the Trade class by taking the String computation out of the constructors and into toString() where it belongs: class Trade implements Serializable { String date; boolean buy; String symbol; int numShares; float price; public Trade (String d, boolean b, int n, String s, float p) { symbol = s; numShares = n; buy = b; date = d; price = p; } public Trade (String tr) { StringTokenizer st = new StringTokenizer(tr, ” “); date = st.nextToken(); if (”Buy”.equals(st.nextToken())) { buy = true; } else { buy = false; } numShares = Integer.parseInt(st.nextToken()); Page 130
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

Comments are closed.