Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:03:35+00:00 2026-06-06T10:03:35+00:00

Is get/put from a non-direct bytebuffer faster than get/put from direct bytebuffer ? If

  • 0

Is get/put from a non-direct bytebuffer faster than get/put from direct bytebuffer ?

If I have to read / write from direct bytebuffer , is it better to first read /write in to a thread local byte array and then update ( for writes ) the direct bytebuffer fully with the byte array ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:03:36+00:00Added an answer on June 6, 2026 at 10:03 am

    Is get/put from a non-direct bytebuffer faster than get/put from direct bytebuffer ?

    If you are comparing heap buffer with direct buffer which does not use native byte order (most systems are little endian and the default for direct ByteBuffer is big endian), the performance is very similar.

    If you use native ordered byte buffers the performance can be significantly better for multi-byte values. For byte it makes little difference no matter what you do.

    In HotSpot/OpenJDK, ByteBuffer uses the Unsafe class and many of the native methods are treated as intrinsics. This is JVM dependent and AFAIK the Android VM treats it as an intrinsic in recent versions.

    If you dump the assembly generated you can see the intrinsics in Unsafe are turned in one machine code instruction. i.e. they don’t have the overhead of a JNI call.

    In fact if you are into micro-tuning you may find that most of the time of a ByteBuffer getXxxx or setXxxx is spend in the bounds checking, not the actual memory access. For this reason I still use Unsafe directly when I have to for maximum performance (Note: this is discouraged by Oracle)

    If I have to read / write from direct bytebuffer , is it better to first read /write in to a thread local byte array and then update ( for writes ) the direct bytebuffer fully with the byte array ?

    I would hate to see what that is better than. 😉 It sounds very complicated.

    Often the simplest solutions are better and faster.


    You can test this yourself with this code.

    public static void main(String... args) {
        ByteBuffer bb1 = ByteBuffer.allocateDirect(256 * 1024).order(ByteOrder.nativeOrder());
        ByteBuffer bb2 = ByteBuffer.allocateDirect(256 * 1024).order(ByteOrder.nativeOrder());
        for (int i = 0; i < 10; i++)
            runTest(bb1, bb2);
    }
    
    private static void runTest(ByteBuffer bb1, ByteBuffer bb2) {
        bb1.clear();
        bb2.clear();
        long start = System.nanoTime();
        int count = 0;
        while (bb2.remaining() > 0)
            bb2.putInt(bb1.getInt());
        long time = System.nanoTime() - start;
        int operations = bb1.capacity() / 4 * 2;
        System.out.printf("Each putInt/getInt took an average of %.1f ns%n", (double) time / operations);
    }
    

    prints

    Each putInt/getInt took an average of 83.9 ns
    Each putInt/getInt took an average of 1.4 ns
    Each putInt/getInt took an average of 34.7 ns
    Each putInt/getInt took an average of 1.3 ns
    Each putInt/getInt took an average of 1.2 ns
    Each putInt/getInt took an average of 1.3 ns
    Each putInt/getInt took an average of 1.2 ns
    Each putInt/getInt took an average of 1.2 ns
    Each putInt/getInt took an average of 1.2 ns
    Each putInt/getInt took an average of 1.2 ns
    

    I am pretty sure a JNI call takes longer than 1.2 ns.


    To demonstrate that its not the “JNI” call but the guff around it which causes the delay. You can write the same loop using Unsafe directly.

    public static void main(String... args) {
        ByteBuffer bb1 = ByteBuffer.allocateDirect(256 * 1024).order(ByteOrder.nativeOrder());
        ByteBuffer bb2 = ByteBuffer.allocateDirect(256 * 1024).order(ByteOrder.nativeOrder());
        for (int i = 0; i < 10; i++)
            runTest(bb1, bb2);
    }
    
    private static void runTest(ByteBuffer bb1, ByteBuffer bb2) {
        Unsafe unsafe = getTheUnsafe();
        long start = System.nanoTime();
        long addr1 = ((DirectBuffer) bb1).address();
        long addr2 = ((DirectBuffer) bb2).address();
        for (int i = 0, len = Math.min(bb1.capacity(), bb2.capacity()); i < len; i += 4)
            unsafe.putInt(addr1 + i, unsafe.getInt(addr2 + i));
        long time = System.nanoTime() - start;
        int operations = bb1.capacity() / 4 * 2;
        System.out.printf("Each putInt/getInt took an average of %.1f ns%n", (double) time / operations);
    }
    
    public static Unsafe getTheUnsafe() {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(null);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
    

    prints

    Each putInt/getInt took an average of 40.4 ns
    Each putInt/getInt took an average of 44.4 ns
    Each putInt/getInt took an average of 0.4 ns
    Each putInt/getInt took an average of 0.3 ns
    Each putInt/getInt took an average of 0.3 ns
    Each putInt/getInt took an average of 0.3 ns
    Each putInt/getInt took an average of 0.3 ns
    Each putInt/getInt took an average of 0.3 ns
    Each putInt/getInt took an average of 0.3 ns
    Each putInt/getInt took an average of 0.3 ns
    

    So you can see that the native call is much faster than you might expect for a JNI call. The main reason for this delay could be the L2 cache speed. 😉

    All run on an i3 3.3 GHz

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get text from text files and put it in a non-editable
From my understanding, each of these methods: get() and put() are atomic. But, when
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
I get a script from a website to put it into my website, but
I just get a script from a website to put it on my own
I'm writing a program where I get information from a page and put it
I have a php script that I've put together from examples on the internet.
I wont to read lines from table to put theme in a list php
I am trying to get put out a simple site, which would pool the
The state monad interface class MonadState s m where get :: m s put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.