简体   繁体   English

JNI和旧的C ++代码

[英]JNI and old C++ code

I am now writing a JNI version of older c/c++ code I wrote earlier in the year, to include in my android application. 我现在正在编写我今年早些时候写的旧版c / c ++代码的JNI版本,包含在我的android应用程序中。 I wrote a desktop application equivalent of what I am writing in both Java and C++, and the difference in performance is prominent, and I want to therefore take advantage of the ndk. 我编写了一个桌面应用程序,相当于我在Java和C ++中编写的内容,并且性能上的差异非常突出,因此我希望利用ndk。

I am extremely new to JNI, and the way I understand things, it seems that I have to do some type mapping; 我对JNI非常陌生,而且我理解事物的方式,似乎我必须做一些类型映射; jdouble to replace double , jint to replace int , and so on. jdouble替换doublejint替换int ,依此类推。 The thing is though, I've tried running some of my old code unchanged, and it worked! 但事情是,我已经尝试将我的一些旧代码保持不变,并且它有效! I even have some STL libraries included ( list and vector ) and I faced no problems. 我甚至包括一些STL库( listvector ),我没有遇到任何问题。 I just included APP_STL := stlport_static in the Android.mk file. 我刚刚在Android.mk文件中包含了APP_STL := stlport_static

So my questions are: is there a performance penalty in doing this? 所以我的问题是:这样做是否有性能损失? Is there any other risk I should be aware of? 我应该注意其他风险吗? What is the disadvantage of using C/C++ primitive types in my jni? 在我的jni中使用C / C ++原语类型有什么缺点? Can I make the code run faster, or should I not bother? 我可以让代码运行得更快,还是我不应该打扰?

Thanks, 谢谢,

NAX NAX

Native types in JNI ( jint , ...) are just typedef aliases of C types. JNI中的本机类型( jint ,...)只是C类型的typedef别名。 They exist for code portability when internal Java representation doesn't exactly match C, namely, regarding bit size. 当内部Java表示与C不完全匹配时,它们存在于代码可移植性,即关于位大小。 So, there shouldn't be any performance overhead in mixing them. 因此,混合它们不应该有任何性能开销。

The risk or disadvantage of mixing them is that, if they turn out to have different bit sizes in some platform, you can lose bits when converting back and forth. 混合它们的风险或缺点是,如果它们在某些平台中具有不同的位大小,则在来回转换时可能会丢失位。 You might thus want to do some extra checks when building the code to make sure it is safe. 因此,您可能希望在构建代码时进行一些额外的检查,以确保它是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM