Women in Technology

Hear us Roar

  Micro-Tuning Step-by-Step
Subject:   Might this not be quicker?
Date:   2002-03-26 05:49:54
From:   godfrer
public boolean checkInteger(String testInteger)
final int length = testInteger.length();

// since we know that the integer must be
// between 30 and 39999 inclusive, the length
// of the string must be between 2 and 5.
if((length < 2) || (length > 5)) return false;

if(testInteger.charAt(0) != '3') return false;

// no need to actaully calculate the int value
// since we know that it starts with 3 and has
// a length of between 2 and 5. Just need to
// check that all the characters are digits

for(int i = 1; i < length; i++)
return false;

// passed all the tests
return true;

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • RE:Might this not be quicker?
    2002-03-29 11:11:48  steveftoth [View]

    Sometimes you have to take a step back from the problem and see what it is that you are really trying to accomplish. This is the major problem with micro-optimising in general. Usually, instead of asking what really need to be done ( like in this case ), we just take what we are given and try to make that faster.