1

In the Android SDK, there is a comment that says it's more efficient to retrieve resources by identifier instead of by name.

Is this the only reason it's discouraged to use getIdentifier()?

I need to programmatically access one of several hundred resources and so far my design makes it easier to access raw resources by name instead of identifier.

CalvinS
  • 615
  • 1
  • 7
  • 14

2 Answers2

6

Because comparing integers is much faster than comparing strings and I would guess that the resources are hashed by their ID, so if you get a resource by ID they can grab only a small subset of resources and perform a fast search. If you search by name they'd have to iterate through all the resources and do a slow string compare.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
2

Yes, getIdentifier() is slower/less efficient than just retrieving the resource by ID, I think it uses reflection to get the identifier from the generated R class. But I'm not aware of any other reason not to use it... Look into array resources, maybe you can use those instead.

cristis
  • 1,986
  • 16
  • 22