为什么我的本机C ++代码在Android上的运行速度比Java慢得多? [英] Why is my native C++ code running so much slower than Java on Android?

查看:167
本文介绍了为什么我的本机C ++代码在Android上的运行速度比Java慢得多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Java代码的某些部分移植到C ++,以加快Android上的计算速度(这是一个物理子例程).我发现本机代码的运行速度比Java代码慢几倍.我以为我的项目配置或数组处理可能有问题,所以我在HelloAndroidJni项目中放入了一个简单的循环来测试原始速度差异,并得到相似的结果.

I ported some portions of my Java code to C++ to speed up calculations on Android (this was a physics subroutine). What I found was that the native code runs several times slower than the Java code. I thought maybe something was wrong with the configuration of my project, or maybe with the array handling, so I put a simple loop in the HelloAndroidJni project to test raw speed difference, and got similar results.

Java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    /* ...generic boilerplate code... */

    TextView tv = (TextView) findViewById(R.id.sample_text);
    int loopCount = 100000;

    //time the native method
    long ticks = System.nanoTime();
    int result = nativeTest(100000);
    long nativeTime = (System.nanoTime() - ticks) / 100000;

    //time the Java method
    ticks = System.nanoTime();
    result = javaTest(100000);
    long javaTime = (System.nanoTime() - ticks) / 100000;

    //present results
    tv.setText("Native=" + nativeTime + "; Java=" + javaTime);
}

Java中的循环:

int javaTest(int count) {
    int result = 0;
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < 100; j++) {
        result += 34432; result++;
        result -= 34431; result--;
    } }
    return result;
}

以及C ++代码:

JNIEXPORT jint JNICALL
Java_com_fringecode_helloandroidjni_MainActivity_nativeTest(
        JNIEnv *env, jobject jThis, jint count) {
    int result = 0;
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < 100; j++) {
            result += 34432; result++;
            result -= 34431; result--;
        } }
    return result;
}

项目的其余部分与HelloAndroidJni示例项目相同.典型运行的结果是Native = 2580 ms,Java = 195 ms.是什么让本地代码的运行速度比Java慢得多?

The rest of the project is identical to the HelloAndroidJni sample project. The result of a typical run is Native=2580 ms, Java=195 ms. What could be making the native code run so much slower than the Java?

顺便说一句,本机代码在模拟器上的运行速度比Java快得多,但是在我的手机(LG V20/Snapdragon 820)上,本机的运行速度要慢得多.

Incidentally, the native code runs much faster than Java on the emulator, but on my phone (LG V20 / Snapdragon 820) the native is much slower.

推荐答案

运行中的Java优化可能会使您的循环与本地循环一样快.另一方面,如果没有APP_OPTIM=release,C ++编译器将生成调试未优化的代码.

Java on the fly optimization may make your loop be as fast as native. On the other hand, without APP_OPTIM=release the C++ compiler will generate debug unoptimized code.

要点是,如果以规范的方式进行编码,实际上Java中的数字处理可能会非常有效.但是毕竟,在C中高效地进行相同的编码也需要纪律.

The takeaway is that actually number crunching in Java may be quite efficient, if coded in disciplined manner. But after all, coding same efficiently in C also requires discipline.

这篇关于为什么我的本机C ++代码在Android上的运行速度比Java慢得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆