Skip to content

Instantly share code, notes, and snippets.

@cberner
Created May 27, 2016 04:54
Show Gist options
  • Save cberner/20dbf68ca88198443e2f534e8f415462 to your computer and use it in GitHub Desktop.
Save cberner/20dbf68ca88198443e2f534e8f415462 to your computer and use it in GitHub Desktop.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.airlift.slice;
import org.openjdk.jmh.runner.RunnerException;
import java.util.concurrent.ThreadLocalRandom;
public class Junk
{
public static int rawArrayAccess(byte[] data)
{
int value = 0;
for (int i = 0; i < data.length; i++) {
value ^= data[i];
}
return value;
}
public static int sliceUnchecked(Slice data)
{
int value = 0;
for (int i = 0; i < data.length(); i++) {
value ^= data.getByteUnchecked(i);
}
return value;
}
public static void main(String[] args)
throws RunnerException
{
byte[] bytes = new byte[1024];
ThreadLocalRandom.current().nextBytes(bytes);
Slice slice = Slices.wrappedBuffer(bytes);
int result = 0;
for (int i = 0; i < 1000_000_000; i++) {
result += rawArrayAccess(bytes);
result += sliceUnchecked(slice);
}
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment