Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- public class Test {
- public static void main(String[] args) {
- float[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- FloatBuffer buffer = ByteBuffer.allocateDirect(data.length * Float.BYTES).asFloatBuffer();
- long start;
- // test 1: flip()
- start = preTime("Entire array, flip()");
- {
- buffer.put(data).flip();
- }
- postTime(start, buffer);
- buffer.clear();
- // test 2: no flip()
- start = preTime("Entire array, no flip()");
- {
- buffer.put(data);
- }
- postTime(start, buffer);
- buffer.clear();
- // test 3: put() with flip()
- start = preTime("Loop through, with put() and flip()");
- {
- for(int i = 0; i < data.length; i++) {
- buffer.put(data[i]);
- }
- buffer.flip();
- }
- postTime(start, buffer);
- buffer.clear();
- // test 3: put() with index, w/o flip()
- start = preTime("Loop through, with put() at index");
- {
- for(int i = 0; i < data.length; i++) {
- buffer.put(i, data[i]);
- }
- }
- postTime(start, buffer);
- buffer.clear();
- // test 5: position(0)
- start = preTime("Entire array, position(0)");
- {
- buffer.put(data).position(0);
- }
- postTime(start, buffer);
- buffer.clear();
- }
- private static long preTime(String label) {
- System.out.print(label+ ": ");
- return System.nanoTime();
- }
- private static void postTime(long start, FloatBuffer buffer) {
- long ns = System.nanoTime() - start;
- System.out.println("took " + ns + "ns: position = " + buffer.position());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement