How instanceof is implemented inside JAVA?

前端 未结 3 528
梦谈多话
梦谈多话 2021-01-13 22:05

Now I\'m writing an ORM Framework and very care about performance.

In this Framework , I have to use instanceof and Class.isAssignableFrom

3条回答
  •  生来不讨喜
    2021-01-13 22:18

    How instanceof is implemented inside JAVA?

    The short answer is that it is platform dependent.

    The long answer is that you should be able to find out how it is implemented by writing a test case that uses instanceof, running it in a loop to ensure it gets JIT compiled, and then dumping and examining the native code.

    However, I don't think this is going to be particularly instructive. What you really want to know is whether instanceof or Class.isAssignableFrom is faster. You can measure this by careful micro-benchmarking.

    FWIW, I predict that you will find that instanceof is faster. (I expect that the JIT compiler would be able to optimize instanceof in ways that it couldn't optimize the reflective version.)

    But lastly, I'd suggest that you don't waste your time with this level of optimization at this stage. Code this using instanceof, and wait until you have some profiling data that tells you that your instanceof usage is really a performance bottleneck. (It is all very well to "care about performance" ... but in reality there are more important things that you need to get right BEFORE performance becomes a key issue.)

提交回复
热议问题