Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From your comments, I conclude that you may be suffering from an interpretive mistake when investigating profiling results. Your profiler's method-level instrumentation might be heavily crippling <code>getClass()</code> and <code>isArray()</code> calls, while being unimpressed by <code>instanceof</code> expressions. In other words, you're probably measuring the measuring overhead of your profiler, here.</p> <p>Besides, in a quick benchmark, I cannot back your claim. I've run the following, very silly test:</p> <pre><code>public class Test { public static void main(String[] args) { final int rep = 10000000; Object[] o = { null, 1, "x", new Object[0], new Object[0][], new int[0], new int[0][] }; // "Warmup" to avoid potential JVM startup overhead long x = 0; for (int i = 0; i &lt; rep; i++) { x+=checkInstanceOf(o); } for (int i = 0; i &lt; rep; i++) { x+=checkIsArray(o); } for (int i = 0; i &lt; rep; i++) { x+=checkClassName(o); } // Actual test long t1 = System.nanoTime(); for (int i = 0; i &lt; rep; i++) { x+=checkInstanceOf(o); } long t2 = System.nanoTime(); for (int i = 0; i &lt; rep; i++) { x+=checkIsArray(o); } long t3 = System.nanoTime(); for (int i = 0; i &lt; rep; i++) { x+=checkClassName(o); } long t4 = System.nanoTime(); System.out.println(t2 - t1); System.out.println(t3 - t2); System.out.println(t4 - t3); } private static int checkInstanceOf(Object[] o) { int i = 0; for (Object x : o) { if (x instanceof Object[]) i++; // Perform some logic else if (x instanceof boolean[]) i++; // to keep the compiler or else if (x instanceof byte[]) i++; // the JVM from optimising else if (x instanceof short[]) i++; // this code away else if (x instanceof int[]) i++; else if (x instanceof long[]) i++; else if (x instanceof float[]) i++; else if (x instanceof double[]) i++; else if (x instanceof char[]) i++; } return i; } private static int checkIsArray(Object[] o) { int i = 0; for (Object x : o) { if (x != null &amp;&amp; x.getClass().isArray()) i++; } return i; } private static int checkClassName(Object[] o) { int i = 0; for (Object x : o) { if (x != null &amp;&amp; x.getClass().getName().charAt(0) == '[') i++; } return i; } } </code></pre> <p>I'm getting:</p> <pre><code>394433000 // instanceof 110655000 // getClass().isArray() 396039000 // getClass().getName().charAt(0) == '[' </code></pre> <p>So you cannot <em>generally</em> claim <code>getClass().isArray()</code> to be slower than a thorough set of <code>instanceof</code> checks. Of course, there is a lot of different ways to rewrite my test, but you get the idea.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload