Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Could you use <code>char[65536][]</code>? where the top or bottom 16 bits is an index to an array of the other 16 bits. This could use less than 4 * X per entry.</p> <p>Lookup</p> <pre><code> private final char[][] bitsArray = new char[65536][]; public int countFor(int num) { int topBits = num &gt;&gt;&gt; 16; int lowerBits = num &amp; 0xFFFF; char[] lowerBitsArray = bitsArray[topBits]; int count = 0; for(char l : lowerBitsArray) if(l == lowerBits) count++; return count; } </code></pre> <p>If the count can never be more than 1, a BitSet is likely to be a better choice. (Possibly an array of BitSet depending on the pattern of data) E.g. if you were to record IP addresses seen, you might not need to worry about 0.<em>, 10.</em>, 127.* or 224-255.* </p> <hr> <p>Whether an <code>int[]</code> or <code>char[]</code> is faster to access including casting to int.</p> <pre><code>public static void main(String... args) { char[] chars = new char[1000000]; for (int i = 0; i &lt; 5; i++) timeSum(chars); int[] ints = new int[1000000]; for (int i = 0; i &lt; 5; i++) timeSum(ints); } private static int timeSum(char[] chars) { long start = System.nanoTime(); int sum = 0; for (char ch : chars) { sum += ch; } long time = System.nanoTime() - start; System.out.printf("Took %,d us to sum %,d chars%n", time / 1000, chars.length); return sum; } private static int timeSum(int[] ints) { long start = System.nanoTime(); int sum = 0; for (int i : ints) { sum += i; } long time = System.nanoTime() - start; System.out.printf("Took %,d us to sum %,d ints%n", time / 1000, ints.length); return sum; } </code></pre> <p>prints</p> <pre><code>Took 5,378 us to sum 1,000,000 chars Took 11,551 us to sum 1,000,000 chars Took 437 us to sum 1,000,000 chars Took 407 us to sum 1,000,000 chars Took 407 us to sum 1,000,000 chars Took 5,539 us to sum 1,000,000 ints Took 532 us to sum 1,000,000 ints Took 530 us to sum 1,000,000 ints Took 511 us to sum 1,000,000 ints Took 507 us to sum 1,000,000 ints </code></pre> <p>My conclusion is that cache efficiency is more important than cast cost.</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.
 

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