title: Does Android NDK give speedup for sending large int[] over sockets?
body: <p>I need to send an array of 500,000 ints over a socket between two Android devices. Currently, I'm spending a lot of time converting the int[] to a byte[] so that Java's socket will accept it (see my previous question <a href="https://stackoverflow.com/questions/12320000/efficiently-send-large-int-over-sockets-in-java">Efficiently send large int[] over sockets in Java</a>, where we determined there's no faster way to do the typecasting in Java). </p>
  
  <p>My question now is, if I take the int[] and pass it through JNI to the Android NDK, can I expect the typecasting to byte[] to go any faster in native code? I know typecasting int* to char* is quite simple in plain-old C, however I'm wondering if the JNI will negate any performance gains.  </p>
  
  <p>Furthermore, once I have a byte[] in my native code, can I efficiently pass it back to my Java code or do I need to implement the socket in C as well?</p>
  
  <p>Edit 1: People have been posting a lot of answers without clicking on the link. <strong>Using ByteBuffers is not a good option</strong>, its actually way slower than mask-and-shift, which is still way slower than my performance critical code needs! That's why I'm asking about the NDK.</p>
  
  <p>Edit 2: I changed the text above to say that C code can cast from int* to char* instead of int[] to byte[]. Hopefully that clarifies the question.</p>
  
  <p>Edit 3: To clarify my use-case, this is a research problem where I distribute a large array of ints across multiple devices and sort the list in parallel. Assume that I have 500,000 ints in Java (doesn't matter where they come from) and I need to get them off the device via a socket as quickly as possible. Answers that say "don't start with an array of ints" aren't helpful. Additionally, my application code needs to be as close to 100% Java as possible. If native typecasting and sockets improve performance, that's ok, but I can't do anything else (i.e. the sort) natively. </p>