Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK performance over regular Java code

I've been playing around with the NDK lately, because UI was in need of an efficient implementation of a computationally intensive algorithm. The NDK documentation states:

Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on. Simply re-coding a method to run in C usually does not result in a large performance increase.

Mine is a signal processing algorithm (adaptive filter, in case you're familiar), computationally intensive computation with millions of linear operations (can't show the code, but millions of linear operations pretty much sums it up). When I implement the same code with JNI and Java, I get practically no performance gain. In fact, it takes the exact same amount of time in either, within uncertainty margin, so now I'm not sure whether I should just use Java for everything else. For comparison, they both take ~8 seconds to complete the task.

Are there any better criteria for when algorithms implemented in JNI work faster than Java? Does anyone have an example in which performance was truly noticeable?

like image 917
Phonon Avatar asked Nov 13 '22 22:11

Phonon


1 Answers

for example: sum natural number from 1 to 1,000,000. the code is as follow:

unsigned int sum = 0;
for(int i = 1; i <= 1000000; ++i) {
    sum += i;
}

if the above code is coded by java, the time took maybe about 200ms. But when coded by c in jni, the time took only about 30 ms. You can hava a try.

like image 90
ameyume Avatar answered Dec 16 '22 13:12

ameyume