In general, working with primitive types directly is going to be more efficient than working with objects because you avoid some of the overhead of creating objects, function calls, etc.
Up vote 4 down vote favorite share g+ share fb share tw.
I'm working on an Android application (in Java, obviously) and I recently updated my UDP reader code. In both versions, I set up some buffers and receive a UDP packet: byte buf = new byte10000; short soundData = new short1000; DatagramPacket packet = new DatagramPacket (buf, buf. Length); socket.
Receive (packet); In the initial version, I put the data back together one byte at a time (it's actually 16 PCM audio data): for (int I = 0; I However, there appear to be new problems with my streaming audio -- perhaps it isn't being handled fast enough -- which doesn't make any sense to me. Obviously, the buffer code is compiling into a lot more than three statements of JVM code, but it sure seemed like a reasonable assumption when I start this that the 2nd version would be faster than the 1st. Patently, I'm not insisting that my code HAS to use Java NIO buffers, but at first glance at least, it DOES seem like a mo' betta' to go about this.
Anybody got any recommendations for a fast, simple Java UDP reader and whether there is a generally accepted "best way"? Thanks, R. Java android nio bytebuffer link|improve this question edited Oct 27 '10 at 22:15Tim Bender5,040514 asked Oct 27 '10 at 21:25Rich589416 54% accept rate.
NIO isn't intended to be faster than "normal" IO, its just more scalable. – skaffman Oct 27 '10 at 21:30 Android is not Java. The syntax looks like Java, but in truth Google simply stole the syntax and is being rightfully sued by Oracle/Sun for creating this messy confusion that an Android application is Java, when it in fact it is not.
– Tim Bender Oct 27 '10 at 22:17 stole the syntax Lol are you serious? Google stole java syntax as much as an author steels the English syntax – Falmarri Oct 27 '10 at 22:39 @Tim Bender what does that have to do with the question? There might be differences between the Dalvik vm vs. standard jvm, but in general if things are slow in the jvm they are likely to also be slow on Android.
Do you have some knowledge of a difference that might affect this particular case? – Mayra Oct 27 '10 at 22:40 @Mayra, the library itself is completely different. Android library implementations!
= Sun library implementations. Also, Java-Like code compiled for Android is not compiled down to Java bytecode but instead compiled down to Dalvik. The point?
This question was originally tagged "Java" when it in fact should be tagged "android". Since Rich is an android developer and not a Java developer he should at least know how to properly tag his questions :) – Tim Bender Oct 27 '10 at 23:53.
In general, working with primitive types directly is going to be more efficient than working with objects because you avoid some of the overhead of creating objects, function calls, etc. There are reasons to use the utility objects other than speed: convenience, safety, etc. The best way to test the difference in this particular case would be to actually measure it. Try out both methods with a large dataset and time it. Then, you can decide if it is worth the benefits in this case.
You can also use Android's profiler to see where your problems really are. See TraceView.
I seem to have touched off a religious war... Sorry about that, and thanks for the rationality. – Rich Oct 28 '10 at 12:29 Some of the NIO stuff is pretty horrid at the moment. Improvements have been made and will start showing up in future releases.
In the mean time, "test & measure" is a good answer. – fadden Oct 29 '10 at 23:52.
I would use DataInputStream for this task, wrapped around a ByteArrayInputStream wrapped around the byte array. Encapsulates the shifting in readShort() without the overheads of ByteBuffer. Alternatively you could read directly into a DirectByteBuffer via a DatagramChannel, rather than using DatagramPacket & DatagramSocket.
At present you're using a mixture of java.net and java.nio. I wouldn't expect major performance differences between these two approaches, but I would expect them both to be faster than your hybrid approach.
Encapsulates the shifting in readShort() without the overheads of ByteBuffer. ReadShort has more overhead than the same operation of a ByteBuffer – bestsss Mar 20 '11 at 19:18 @bestsss: 'overheads' plural: especially creation overheads. It is those I am referring to.
– EJP Mar 22 '11 at 11:51 direct ByteBuffer has some allocation (and deallocation, due to finalizer based one) but ByteArrayInputStream has the same allocation cost as ByteBuffer. Wrap and as a bonus all the methods are synchronized, plus ByteBuffer tends to enjoy extra intrinsic by the JIT. Also you do not create a direct ByteBuffer for each packet but once per socket (or take it off a pool).
– bestsss Mar 22 '11 at 11:58 @bestsss: you're getting pretty far off the point. If ByteBuffer.readShort() is more efficient than DataInputStream.readShort(), given the other overheads of the latter, whatever they may be comparable to, I would like to see some actual evidence. – EJP Mar 28 '11 at 9:56 @EJP, write your own test, or just look at the impl.
– bestsss Mar 28 '11 at 11:27.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.