Android NDK vs iOS - performance issue [closed]

心不动则不痛 提交于 2020-01-04 06:08:07

问题


I've been looking around for solution at least 2 days unsuccesfully so as my last hope I decided to ask it here.

Here at work we have a C++ code base using OpenCV that we want to run both on iOS and Android. It turned out that the whole thing runs slower on Android and I can't find out the reason. After profiling we got to know that the method that invokes the C++ code is the problem which is the exact same on both platforms. On Android(Samsung Galaxy S4) it takes 140-150 ms to execute while on iOS(iPhone 5) it's under 70ms. I've read some articles about optimizing native code and using different local_cflags but didn't seem to help.

Is this a fact that needs to be admitted or is there a solution? Thank you in advance, Mike


回答1:


Your experience correlates well with mine. In my experience using OpenCV on iOS and Android (in a Nexus 4 in my case):

  1. Android is generally slower if you only use single-threaded code. Apple CPU cores are faster than any core I've tested on Android phones (see the many phone reviews available online), while latest Android phones have 4 or more cores. On iOS, OpenCV uses GCD to run a few algorithms in parallel, but on Android it doesn't use OpenMP (which is the alternative, but only works with GCC 4.x, and not Clang). Sadly, using OpenMP outside the main thread is a pain. This bug is still present in r10 of the NDK, so either you recompile the toolchain with the patches, or you are stuck on the main thread, which is not the best option for heavy computation.

  2. OpenCV on Android, by default, comes compiled with Thumb instructions, which are slower. I suggest recompiling it setting ARM mode ON, and NEON.

  3. Autovectorization flags. If you are using GCC on the NDK, you have to use -O3, plus -funsafe-math-optimizations to enable autovectorization with NEON.

  4. Throttling of the CPU frequency. My Nexus 4 seems to throttle the CPU frequency more enthusiastically than iOS. We've seen substantial swings in timings on Android code that runs at very stable timings on iOS, and the only reason we can think of is the CPU frequency. Renderscript (see this answer) maxes out the CPU frequency, but the battery life will suffer (and you have to rewrite the code).



来源:https://stackoverflow.com/questions/25641609/android-ndk-vs-ios-performance-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!