1

I was wondering what would be the best way to save some resources and memory in my test app. I know that creating objects eats up memory and slows down the app and in the app the code requires an switch case of String values. So what would be better? An if else statement for all string values to assign each of them an integer tag and using switch case or to create an Enumerator and use switch directly?

Number of entries = 40-50.

Exorcist
  • 252
  • 2
  • 3
  • 15
  • 1
    How often do you do this lookup on the same set of strings? If more than maybe 4 times then the way to go is a hash table. (Don't obsess about creating objects -- dealing with the UI churns through massive numbers of objects, so a few more here and there is no biggie.) – Hot Licks Mar 13 '12 at 11:55

2 Answers2

3

Don't worry about it. The new Java 7 switch statement automatically does this for you.

A switch with String cases is translated into two switches during compilation. The first maps each string to a unique integer—its position in the original switch. This is done by first switching on the hash code of the label. The corresponding case is an if statement that tests string equality; if there are collisions on the hash, the test is a cascading if-else-if. The second switch mirrors that in the original source code, but substitutes the case labels with their corresponding positions. This two-step process makes it easy to preserve the flow control of the original switch.

Just sit back, take a sip of coffee, and let the compiler+JVM do all the heavy lifting for you. Making micro-optimizations in cases like this will more likely hurt performance than help.

Community
  • 1
  • 1
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
3

I know that creating objects eats up memory and slows down the app ...

This generalization is problematic:

Yes, creating an object consumes memory and takes time, but it doesn't necessarily slow down the app. It depends on what the alternative to creating the object is. It could well be that the alternative slows down the app more.

And even assuming that the "create an object" version uses more resources than the alternative, there's a good chance that the difference won't be significant.

What you seem to be doing here is premature optimization. My advice is:

  • let the JVM deal with the optimization (it can probably do it better than you anyway),
  • leave any hand optimization to later, and only do it if the measured performance of the app actually warrants it, and
  • use a CPU or memory profiler to guide you as to the parts of your code where going to the effort of hand optimizing is likely to have a good payoff.

As to the specifics of your use-case, it is not clear what the most efficient solution would be. It depends on things like how the strings are formed, how many branches there are in the switch statement, and things like that. It is hard to predict without profiling the application with realistic input data.

A third option that you didn't mention is to use an enum instead of a String or int. That will be neater than implementing the String to int mapping using a pre-populated HashMap or something.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Can you have a look at this [question](http://stackoverflow.com/questions/9674891/two-spinners-in-one-activity)? – Exorcist Mar 13 '12 at 12:06
  • I did mention `enum` in the last line as `Enumerator` and the number of branches would be same as that of entries,which I also mentioned as 40-50. Anyways,thank you! – Exorcist Mar 14 '12 at 13:35