8

I'm currently working on an AirPlay receiver for a subpart of an android application. I am using the following framework:

https://github.com/pentateu/DroidAirPlay

While this works great on some mid range devices such as the miPad, we need to get this running on a low spec custom device. The custom device is decoding the airplay packets at 10x to 20x slower than the miPad. As a result, the audio packets lose time synchronisation and due to the time taken to decode the packets, the audio can never re-sync.

I was looking into some other airplay receiver apps on the Play Store and from what I can see they tend to be based on Shairport (https://github.com/abrasive/shairport) for the airplay receiver side of things.

**Note: ** the Shairport based frameworks do not seem to suffer the synchronisation issue on the low end device.

The framework I am using is heavily based on the Shairport framework apart from that it is written in Java.

For decoding data, is C/C++ far superior than Java?

If so, would directing the decoding part of the DroidAirPlay framework via a C or C++ implementation using the NDK give me a large boost to performance?

Thanks in advance

Matt

Matthew Cawley
  • 2,828
  • 1
  • 31
  • 42

1 Answers1

0

While it is true that Java compiles to bytecode that runs in a virtual machine, it may not necessarily be slower (or faster) than a natively compiled executable, whether C/C++ or not. It all depends on the program!

There's a number of reasons why in this case Java may be slower:

  • The decoding implementation might just be poorly coded/optimized? (which isn't really Java's fault)
  • The Java compiler may generate sub-optimal code for the JVM.
  • Some of Java's language constructs are just too slow for the speed/resource demands placed on it here.
  • The JVM just is another abstraction layer and the culprit
  • The garbage collection is in on it?!

(I have to note here that I'm not an expert on Java!)

However, I still wouldn't go so far as to call Java intrinsically slower than C or C++. I'm sure you can find many-a benchmarks and tests on the internet comparing one language to another, and some make claims to a certain degree (out of pride and ego?). But those tests are only specific cases, usually testing specific aspects of a larger language (hash-map lookup performance for instance!).

LLVM had a three part blog post on C and why undefined behavior allows the compiler to generate still correct but more efficient code at the cost of inferring run-time safety checking or deciding that i+1 always comes after i, totally ignoring the existence of integer overflow. If the programmer is not careful, this can have disastrous consequences.

In the words of Bjarne himself in Abstraction and the C++ machine model:

C++ was designed to be a systems programming language and has been used for embedded systems programming and other resource-constrained types of programming since the earliest days.

As such, I believe C and C++ can be pushed further than Java because of this undefined behavior and less restrictions placed on it. (And there's also the inline assembly bit, but that's not strictly C!)

Xunie
  • 437
  • 4
  • 21