4

I'm writing code in the Java ME environment, so speed is absolutely an important factor. I have read several places that reflection of any sort (even the very limited amounts that are allowed on java ME) can be a very large bottleneck.

So, my question is this: is doing String.class.getName() slow? What about myCustomObject.getClass().getName()? Is it better to simply replace those with string constants, like "java.lang.String" and "com.company.MyObject"?

In case you're wondering, I need the class names of all primitives (and non-primitives as well) because Java ME does not provide a default serialization implementation and thus I have to implement my own. I need a generic serialization solution that will work for both communication across the network as well as local storage (RMS, but also JSR-75)

Edit

I'm using Java 1.3 CLDC.

gnat
  • 6,213
  • 108
  • 53
  • 73
Ring
  • 2,249
  • 5
  • 27
  • 40
  • 6
    Have you tried benchmarking it? – CookieOfFortune Jul 31 '12 at 22:13
  • 4
    If you know the class of an object, there is no point in asking it. String.class.getName() can be replaced by "java.lang.String". If you don't know the type of an object, how could you know without asking? Write first. Measure. And if and only if you have a problem, identify where it comes from, and optimize where needed. – JB Nizet Jul 31 '12 at 22:13
  • I want to benchmark it, but my project is currently in a state of mass disarray, its going to take me hours (maybe days) to get this code to a state where it compiles and runs again. For now i'm going to use constants (thanks guys), but i'll be back later with benchmarking information. – Ring Jul 31 '12 at 22:24
  • what for are you planning to serialize? remote communication or persistence to rms / file or something else? – gnat Aug 01 '12 at 14:04
  • @gnat I'm writing a generic serialization solution that will work for both communication across the network as well as local storage (RMS, but also JSR-75). The makers of Floggy did a very nice job of implementing serialization, but unfortunately they unnecessarily tied it to RMS. – Ring Aug 01 '12 at 22:23
  • 1
    "I'm writing code in the Java ME environment, so speed is absolutely an important factor. " -- probably false at this level of optimization. Profiling/benchmark results are the only reason speed is ever important. – djechlin Aug 01 '12 at 22:27

3 Answers3

5

String.class.getName() would be not slow because its value will be loaded before executed.i.e compiler will put its value before line will execute. myCustomObject.getClass().getName() would be bit slower then previous as it will be retrieved at time for execution

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
  • 2
    Verified on a JME emulator, the time is negligable for doing String.class.getName() – Ring Aug 07 '12 at 20:40
1

Reflection is not unnaturally slow; it's just as slow as you'd expect, but no slower. First, calling a method via reflection requires all the object creation and method calling that is obvious from the reflection API, and second, that if you're calling methods through reflection, Hotspot won't be able to optimize through the calls.

Calling getClass().getName() is no slower than you'd expect, either: the cost of a couple of virtual method calls plus a member-variable fetch. The .class version is essentially the same, plus or minus a variable fetch.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

I can't speak for Java ME, but I'm not surprised at the overhead by using reflection on a resource constrained system. I wouldn't think it is unbearably slow, but certainly you would see improvements from hard-coding the names into a variable.

Since you mentioned you were looking at serialization, I'd suggest you take a look into how its done in the Kryo project. You might find some of their methods useful, heck you might even be able to use it in Java ME. (Unfortunately, I have no experience with ME)

Jyro117
  • 4,519
  • 24
  • 29