Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Field;
- public class scan {
- public static void main(String[]args) {
- bench();
- bench();
- bench();
- bench();
- bench();
- bench();
- System.out.println(cnt);
- }
- public static void bench() {
- long t = System.currentTimeMillis();
- for (int i = 0; i < 1000000; i++) {
- perform();
- }
- System.out.println(System.currentTimeMillis() - t);
- }
- public static void perform() {
- scanByByte(xbytes, 0, xbytes.length);
- //scanByLong(xbytes, 0, xbytes.length);
- //scanByInt(xbytes, 0, xbytes.length);
- }
- static byte[]xbytes = new byte[1000];
- static {
- xbytes[50] = -1;
- xbytes[400] = -3;
- xbytes[900] = -3;
- }
- static int cnt = 0;
- static void scanByByte(byte[]bytes, int p, int end) {
- while (p < end) if (bytes[p++] < 0) cnt++;
- }
- protected static final sun.misc.Unsafe unsafe = sun.misc.Unsafe.class.cast(getUnsafe());
- private static final Object getUnsafe() {
- try {
- Class sunUnsafe = Class.forName("sun.misc.Unsafe");
- Field f = sunUnsafe.getDeclaredField("theUnsafe");
- f.setAccessible(true);
- return f.get(sunUnsafe);
- } catch (Exception ex) {
- throw new RuntimeException(ex);
- }
- }
- static final long offset = unsafe.arrayBaseOffset(byte[].class);
- static final long LONG_MASK = 0x8080808080808080L;
- static final int BY_LONG = 8;
- static final int INT_MASK = 0x80808080;
- static final int BY_INT = 4;
- static void scanByLong(byte[]bytes, int p, int end) {
- while (p < end - BY_LONG) {
- if ((unsafe.getLong(bytes, offset + p) & LONG_MASK) == 0) {
- p += BY_LONG;
- } else {
- int end2 = p + BY_LONG;
- while (p < end2) if (bytes[p++] < 0) cnt++;
- }
- }
- while (p < end) if (bytes[p++] < 0) cnt++;
- }
- static void scanByInt(byte[]bytes, int p, int end) {
- while (p < end - BY_INT) {
- if ((unsafe.getInt(bytes, offset + p) & INT_MASK) == 0) {
- p += BY_INT;
- } else {
- int end2 = p + BY_INT;
- while (p < end2) if (bytes[p++] < 0) cnt++;
- }
- }
- while (p < end) if (bytes[p++] < 0) cnt++;
- }
- }
Add Comment
Please, Sign In to add comment