Reputation: 329
I am writing a code in java , where every time a user types 1 character it checks if it is one of the special characters
char a = getFromInput();
if(a = '$'){
//do something
}else if(a = '@'){
//do something
}else if(a = '#'){
//do something
}else if(a ='~'){
//do something
}else if(a ='^'){
//do something
}else if(a ='&'){
//do something
}... // the if statements go on
However It came to me that this is inefficient since it uses to much if statements . To solve this, I came up with the idea to make an array of bytes with the length of the unicode index of the last special character
byte[] bytes = new byte[1000]
// the length 1000 is because I thought most of the special characters are
// index of less that 1000 in unicode UTF-8 mapping
Functor[] functors;
// functors are just objects that i made that contain function
char a = getFromInput();
if(byte[a] >100){
// do nothing when byte[char] is larger that 100
}else {
functors[byte[a]].func();
}
So basically what i'm trying to do is using the char variable as index of the array. What i'm curious is
Would using more that 100 if statements for every character the user inputs be okay? are there any suggestions ?
I know making an array would cost more memory but would it be better than using multiple (tons of ) if statement with respect to the performance ?
Upvotes: 0
Views: 69
Reputation: 719307
Would using more that 100 if statements for every character the user inputs be okay? are there any suggestions ?
It is up to you to decide what is "okay" ... unless you provide us with the criteria.
However, there are a variety of other alternatives to lots of if
statements:
switch
statementif
statements that test character ranges rather than individual charactersSomeFunction
where the character is the array indexSomeEnum
) that you then switch onPairOfCharAndFunction
class sorted on the character field that you search using binary searchHashMap<Character, SomeFunction>
TreeMap<Character, SomeFunction>
or TreeMap<Character, SomeEnum>
that you use to implement range-based checking: hint the floorEntry
method.And so on.
I know making an array would cost more memory but would it be better than using multiple (tons of ) if statement with respect to the performance ?
Yes it would, provided that the array (or similar mapping data structure; see above) only needs to be set up once.
It is theoretically possible for the Java JIT compiler to turn a sequence of if
tests into the machine equivalent of a switch
in some circumstances, but I don't think it does.
... would a switch statement be generally faster than if statements?
Generally yes. If not, the JIT compiler will translate the switch
into a sequence of if
statements. (In fact, there is a time versus code-space trade-off here.)
Upvotes: 1
Reputation: 45005
You should check range of characters instead of checking one character after the other. Something like
if (a < 'a' && a > 'z') {
// do something
}
Upvotes: 1