Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize a native code with android-ndk (Speed Optimization)

I'm compiling a native code using cygwin and Windows7. I got many optimization tips on Internet.

APP_OPTIM := release
ndk-build NDK_DEBUG=0
-DNDEBUG
LOCAL_CFLAGS += -O2

But I can't understand exactly how to set these on Application.mk and Android.mk. I tried many cases by applying the above tips. but, I don't think that the optimization is applied in my native code.

Application.mk

APP_PROJECT_PATH := $(shell pwd)
APP_MODULES := native_lib
APP_OPTIM := release
APP_BUILD_SCRIPT := Android.mk
APP_ABI := armeabi

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := lib/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := nativeC.c \
                   AES/main.c \
                   AES/aes.c \  

LOCAL_C_INCLUDES := ./lib                  
LOCAL_SHARED_LIBRARIES := crypto
LOCAL_CFLAGS += -O2
LOCAL_CFLAGS += -march=armv6 -marm -mfloat-abi=softfp -mfpu=vfp
LOCAL_LDLIBS += -ldl
include $(BUILD_SHARED_LIBRARY)

I hope many comments.


In Addition,

First, I tried to compare the cases between with the above flag and without it. (e.g. I compiled my program with APP_OPTIM := release in Application.mk, and then I compiled without it or with APP_OPTIM := debug, again.) But, I cannot see any change of the running speed.

Second, this is the most important, My program compare the speed of two modules. (For convenience, I call them module A, B) Module A is prebuilt (which is libcrypto.so in Android.mk). And I want to apply optimization into Module B. First of all, I compared the speed test of module A and B in PC (Visual Studio 2010). When I tried module A and B in the PC, the module B is faster than A. (Of course, I precompiled the module A and I use it by calling the library.) Now I'm porting the my program for PC into it for Android. But in Android, the module B is too slower than A.

Therefore, I concluded that this is not optimized.

In summary,

  1. I compared the speed between with the flag and without it.
  2. When running this program in PC, the precompiled module A is faster than B, But in Android, it's totally opposite.

Do you thnk what my program's problem is ? Thank you in advance.

like image 696
user2642459 Avatar asked Aug 25 '13 07:08

user2642459


1 Answers

APP_OPTIM := release

Put the line APP_OPTIM := release into your Application.mk file

ndk-build NDK_DEBUG=0

Just pass the NDK_DEBUG=0 as a parameter to ndk-build script. You don't need it once you define APP_OPTIM := release.

-DNDEBUG

This should go into your LOCAL_CFLAGS:

LOCAL_CFLAGS += -DNDEBUG

LOCAL_CFLAGS += -O2

This is not required actually, since the Android NDK already defines -O2 optimization.

like image 69
Sergey K. Avatar answered Nov 04 '22 16:11

Sergey K.