Skip to content

Instantly share code, notes, and snippets.

@toddlipcon
Created August 7, 2018 20:52
Show Gist options
  • Save toddlipcon/4b6ddf0febcc3620ccdf68e5f11c6c83 to your computer and use it in GitHub Desktop.
Save toddlipcon/4b6ddf0febcc3620ccdf68e5f11c6c83 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Oracle nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.sample;
import java.nio.ByteBuffer;
import org.openjdk.jmh.annotations.Benchmark;
public class MyBenchmark {
private static class FileDesc {
private final ByteBuffer bb;
int bbPos;
FileDesc(ByteBuffer bb) {
bbPos = bb.getShort(bb.position()) + bb.position();
this.bb = bb;
}
public int getVal() {
return bb.getInt(bbPos);
}
}
private static class FileDesc2 {
private final byte[] bb;
int bbPos;
FileDesc2(byte[] bb) {
bbPos = getShort(bb, 0);
this.bb = bb;
}
public int getVal() {
return getInt(bb, bbPos);
}
public static short getShort(final byte[] b, final int offset) {
return (short) ((b[offset + 1] & 0xFF) | (b[offset] << 8));
}
public static int getInt(final byte[] b, final int offset) {
return (b[offset + 0] & 0xFF) << 24 |
(b[offset + 1] & 0xFF) << 16 |
(b[offset + 2] & 0xFF) << 8 |
(b[offset + 3] & 0xFF) << 0;
}
}
static final byte[] foo = new byte[]{0, 4,3,4,5,6,7,8,9};
@Benchmark
public int testMethod() {
return new FileDesc(ByteBuffer.wrap(foo)).getVal();
}
@Benchmark
public int testWithBytes() {
return new FileDesc2(foo).getVal();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment