Drawing dashed lines in Swing can be surprisingly slow, and worse, the default implementation seems to be sub-optimal. Of course, for most apps, this isn’t a big deal, but performance-sensitive apps that draw a lot of dashed lines (or draw them often) can suffer. The first thing to optimize is to only draw the portion […]
Tag Archives: java
Overhead of the modulo operator
Posted December 6, 2012 – 10:14 pmWhile optimizing a number-crunching algorithm, I noticed (almost by accident) that a large amount of time was spent performing modulo/remainder operations (%) used for wrapping around the ends of arrays. I found that changing i = (i + 1) % n; to i++; while(i >= n) { i -= n; } sped up my code. […]
Unit testing graphics code
Posted April 1, 2011 – 6:07 pmI recently worked on a project that involved heavy use of custom graphics. Specifically, lots of lines. The code was sufficiently important and complex that it needed to be unit tested. However, I had never unit tested drawing code before. A simple strategy would be to paint to an image, then compare the result with […]
Plain TCP beats RMI
Posted November 25, 2010 – 11:43 pmI’ve been working on a class project that involves a cluster of worker machines. Just to throw something quick together, I thought I’d use RMI. Since I could ensure that every machine was running byte-for-byte identical programs, I didn’t think I’d run into class loading issues. However, I’ve never had good luck with RMI, and […]
HttpServletRequest cheat sheet
Posted January 1, 2010 – 2:44 pmMany HttpServletRequest properties can be confusing, so I decided to create this cheat sheet. (Properties can have similar names, some properties contain delimiters and some don’t, etc.) Let’s say your domain is domain.com, and it points to a router which forwards the request to port 8080 on a server called bob with a local IP […]
The Java DOM API sucks
Posted December 23, 2009 – 7:55 pmHow it sucks I recently wrote some code to filter nodes from a DOM and had issues with NullPointerExceptions: NodeList nl = doc.getElementsByTagName(“mytag”); for(int i = 0, n = nl.getLength(); i < n; i++) { Element elt = (Element) nl.item(i); if(…) { elt.getParentNode().removeChild(elt); } } The problem was that NodeLists are live, and removing elements […]
Double.equals pitfalls
Posted September 11, 2009 – 12:12 pmIt turns out that Double.equals is not always the same as == with two double primitives. Case in point: double a = Double.NaN; System.out.println(a == a); Double b = new Double(a); System.out.println(b.equals(b)); This code will print out false and true. What’s going on? Basically, if you look at the Javadoc for the equals method, it […]
Using scripts to hide complexity and only making it worse
Posted August 25, 2009 – 9:29 pmI recently had to work with software that is customizable using Jython script. As much as I like both Java and Python, this environment was a nightmare. The problem was the inability to write Python in the Python script. First off was the old version of Python. The script was limited to version 2.1 (8 […]