Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 65 66 67 68 69 70 < 71 > 72 73 74 75 76 77 .. 166 >> Next

188 genericAlert.setString("ERROR: reason: " + 2
t.toString());
189 display.setCurrent(genericAlert);
190 if (t instanceof OutOfMemoryError)
191 {
192 Runtime r = Runtime.getRuntime();
193 long free = 0, freed = 0;
194 int trys = 0;
195 while ( (freed += (r.freeMemory() - free)) > 0 && 2
trys < 20)
196 {
197 free = r.freeMemory();
198 System.gc();
199 trys++;
200 }
201 if (debug) System.out.println("Freed " + freed + 2
" bytes.");
202
203 }
204 }
205 }
Listing 22.5 (continued)
The event handler demonstrates three optimizations:
Reduce String Comparisons. Lines 120 and 125 demonstrate comparing against a reference instead of using String comparison.
Lines 132 and 185 again demonstrate using lazy instantiation.
J2ME Performance and Pitfalls 189
Handle OutOfMemoryError. This error is much more common in small footprint devices and must be handled explicitly, like in lines 190 to 200.
207 private final void showDetails(String product, String xmlBuf)
208 {
209 final String ELEMENT_DETAILS = "Details";
210 final String ELEMENT_AUTHORS = "Authors";
211
212 // lazy instantiation
213 if (detailsScreen == null)
214 {
215 // details text box
216 detailsScreen = new TextBox("Details", "", 1024, 2
TextField ANY);
// removed adding the Commands to detailsScreen no change.
220 }
221
222 ticker.reset("Started Timer in showDetails()");
223
224 // clear the text box
225 detailsScreen.delete(0, detailsScreen.size());
226
227 // display tagName : value
228 // first, find the product
229 int prodldx = xmlBuf.indexOf(product);
230 if (prodldx >= 0)
231 {
232 int productCount = products.size();
233 if (updatelndexes)
234 {
235 if (detaillndexes == null)
236 detaillndexes = new int[MAX_RECORDS];
237 int tmpldx = 0;
238 // this loops needs to count up
239 for (int i=0;i < productCount; i++)
240 {
241 String tgt = "<" + ELEMENT_DETAILS;
242 detailIndexes[i] = xmlBuf.indexOf(tgt, tmpldx);
243 tmpldx = detailIndexes[i] + 1;
244 }
245 }
246
247 updateIndexes = false;
248 int detailIdx = -1;
249 for (int i=productCount-1; i >= 0; i)
250 {
251 if (detailIndexes[i] < prodIdx)
252 {
253 detailIdx = i;
254 break;
255 }
256 }
Listing 22.5 (continued)
190 Item 22
257
258 int startldx = detailIndexes[detailIdx];
259 int endldx = ( (detailldx + 1) < detaillndexes.length 2
)? detailIndexes[detailIdx + 1] : xmlBuf.length();
260
261 int traverseIdx = startIdx + 1;
262 while (traverseldx < endldx)
263 {
264 // find a tag
265 int tagStartldx = xmlBuf.indexOf('<', traverseldx)
266 int tagEndldx = xmlBuf.indexOf('>', tagStartldx);
267 String tag = xmlBuf.substring(tagStartIdx+1, 2
tagEndldx) ;
268 if (tag.equals("/" + ELEMENT_DETAILS))
269 break;
270
271
272 // now get the tag contents
273 int endTagStartldx = xmlBuf.indexOf("</" + tag, 2
tagEndldx) ;
274 String contents = xmlBuf.substring(tagEndIdx + 1, 2
endTagStartldx);
275
276 if (!tag.equals(ELEMENT_AUTHORS))
277 {
278 detailsScreen.insert(tag + ":", 2
detailsScreen.size());
279 detailsScreen.insert(contents + "\n", 2
detailsScreen.size());
280 }
281
282 traverseIdx = endTagStartIdx+1;
283 }
284
285 // set the display to the results
286 display.setCurrent(detailsScreen);
287 }
288 ticker.printStats("Method showDetails()");
289 }
Listing 22.5 (continued)
The method showDetails() contains five optimizations:
Line 207 demonstrates declaring methods as final for faster access.
Line 213 again demonstrates lazy instantiation.
Use ArraysInstead of Objects. Line 236 uses an integer array instead of a Vector to store indexes, and it also guesses the maximum size so as to not have to resize the array. These optimizations could be used further in the code to gain additional speed and memory conservation.
Iterate Loops Down to Zero. Comparing against zero is the fastest, so coding loops to count down instead of up is more efficient. Lines 249 and 259 demonstrate this.
J2ME Performance and Pitfalls 191
Only Code the Necessary Functionality. We have abandoned the kxml DOM implementation for performing the minimal number of string comparisons and substrings that we need. You will see this again used in doAmazonSearch().
291: private final void doAmazonSearch(int page, String targets) 2
throws Exception, IOException
292: {
293: final String ELEMENT_PRODUCT_NAME = "ProductName";
294: 295: final String LITE_FORMAT = "lite";
296: 297: ticker.reset("Started Timer in doAmazonSearch()");
298: // get the operation
299: int idx = opsChoice.getSelectedIndex();
300: 301: String op = opsChoice.getString(idx);
302: // get the mode
303: idx = modeChoice.getSelectedIndex();
304: String mode = modeChoice.getString(idx);
305: // static method is fastest 2
306: String sURL = MicroAmazonHttpGet.createURL(op, targets,
mode, 307: LITE_FORMAT, "" + page);
Previous << 1 .. 65 66 67 68 69 70 < 71 > 72 73 74 75 76 77 .. 166 >> Next