dagblog.
← Archive

The height of inefficiency


Some programming “gems” deserve to be put on display. One such gem is this little piece of magic code. Your jaw will drop when you read it. I privately dubbed it “the height of inefficiency,” and I’m not particularly sensitive.

private boolean canConvertValue(String fieldName) {

 for (int i = 11000; i < 16000; i++)

if (fieldName.compareTo(“Z” + i) == 0) return false;

 // pokud nic nenajdu, tak muzu konvertovat

return true;

}

To make matters worse, the way this method gets called is highly inefficient. fieldName is the name of a database column, and the method is called for every row returned by the result set. For each row processed it’s called about 100 times, one hundred in words. In 50 minutes of CPU profiling, 4.75% of the machine’s time was spent in this method. In total, this method alone took 270 seconds. Fortunately, the SQL itself was called only thirty-three times in those 50 minutes.

Medium version

Related articles

Related CZ Podcast episodes