{"id":4204,"date":"2018-03-19T09:00:57","date_gmt":"2018-03-19T00:00:57","guid":{"rendered":"https:\/\/www.sria.co.jp\/blog\/?p=4204"},"modified":"2018-03-14T21:35:31","modified_gmt":"2018-03-14T12:35:31","slug":"4204","status":"publish","type":"post","link":"https:\/\/www.sria.co.jp\/blog\/2018\/03\/4204\/","title":{"rendered":"Android NDK\u3067Java\u3068C++\u306e\u901f\u5ea6\u3092\u6bd4\u8f03\u3057\u3066\u307f\u307e\u3057\u305f"},"content":{"rendered":"<p>\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059\u3002\u958b\u767a\u90e8\u306e\u5e84\u5b50\u3067\u3059\u3002<\/p>\n<p>\u304a\u4ed5\u4e8b\u3067\u521d\u3081\u3066Android NDK\u306b\u89e6\u308c\u308b\u6a5f\u4f1a\u304c\u3042\u308a\u3001C++\u306e\u30cd\u30a4\u30c6\u30a3\u30d6\u30b3\u30fc\u30c9\u304cJava\u30b3\u30fc\u30c9\u306b\u6bd4\u3079\u3066\u3069\u306e\u304f\u3089\u3044\u65e9\u3044\u306e\u304b\u77e5\u308a\u305f\u3044\u3068\u601d\u3063\u3066\u3001\u901f\u5ea6\u6bd4\u8f03\u306e\u5b9f\u9a13\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u305b\u3063\u304b\u304f\u306a\u306e\u3067\u30d6\u30ed\u30b0\u30cd\u30bf\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u691c\u8a3c\u74b0\u5883<\/h2>\n<ul>\n<li>Mac macOS 10.13<\/li>\n<li>Android Studio 2.3.3<\/li>\n<li>Gradle 3.3<\/li>\n<li>Gradle Plugin 2.3.3<\/li>\n<li>JDK 8<\/li>\n<li>\u7aef\u672b Nexus5 (Android 6.0.1)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u6bd4\u8f03\u3057\u305f\u51e6\u7406<\/h2>\n<p>\u6307\u5b9a\u3057\u305f\u6570\u5024\u4ee5\u4e0b\u3067\u3001\u6700\u5927\u306e\u7d20\u6570\u3092\u8868\u793a\u3059\u308b\u51e6\u7406\u3092\u66f8\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u3042\u307e\u308a\u52b9\u7387\u3092\u8003\u3048\u306a\u3044\u3067\u30d0\u30d0\u3063\u3068\u66f8\u3044\u305f\u306e\u3067\u3001\u3042\u307e\u308a\u4e2d\u8eab\u3067\u53e9\u304b\u306a\u3044\u3067\u304f\u3060\u3055\u3044&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>\u307e\u305a\u30cd\u30a4\u30c6\u30a3\u30d6\u30b3\u30fc\u30c9(C++)\u3067\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \">#include &lt;jni.h&gt;\r\n\r\nextern \"C\"\r\nJNIEXPORT jlong JNICALL Java_jp_co_sria_ndktest03_MainActivity_getPrimeNumbersNative(JNIEnv *env, jobject thiz, jlong jlimit) {\r\n    long limit = (long)jlimit;\r\n    long max = 2;\r\n    for (long i = 2; i &lt;= limit; i++) {\r\n        bool isPrime = true;\r\n        for (long j = 2; j &lt; i; j++) {\r\n            if (i % j == 0) {\r\n                isPrime = false;\r\n                break;\r\n            }\r\n        }\r\n        if (isPrime &amp;&amp; max &lt;= i) {\r\n            max = i;\r\n        }\r\n    }\r\n    return max;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6b21\u306bJava\u3067\u66f8\u3044\u305f\u30b3\u30fc\u30c9\u304c\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">public long getPrimeNumbers(long limit) {\r\n    long max = 2;\r\n    for (long i = 2; i &lt;= limit; i++) {\r\n        boolean isPrime = true;\r\n        for (long j = 2; j &lt; i; j++) {\r\n            if (i % j == 0) {\r\n                isPrime = false;\r\n                break;\r\n            }\r\n        }\r\n        if (isPrime &amp;&amp; max &lt;= i) {\r\n            max = i;\r\n        }\r\n    }\r\n    return max;\r\n}<\/pre>\n<p>boolean\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u3068JNI\u95a2\u9023\u306e\u30b3\u30fc\u30c9\u4ee5\u5916\u306f\u540c\u3058\u3067\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3061\u306a\u307f\u306bJava\u304b\u3089\u547c\u3073\u51fa\u3059\u51e6\u7406\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002\u753b\u9762\u306b\u914d\u7f6e\u3057\u305f\u30dc\u30bf\u30f3\u306e\u30af\u30ea\u30c3\u30af\u30a4\u30d9\u30f3\u30c8\u3067\u8a08\u6e2c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">@Override\r\npublic void onClick(View view) {\r\n    long limit = 10000L; \/\/ \u30c1\u30a7\u30c3\u30af\u5bfe\u8c61\u306e\u6570\u5024\r\n    long startMSec = System.currentTimeMillis(); \/\/(1)\r\n    long max;\r\n    if (native) {\r\n        max = getPrimeNumbersNative(limit);\r\n    } else {\r\n        max = getPrimeNumbers(limit);\r\n    }\r\n    long endMSec = System.currentTimeMillis();   \/\/(2)\r\n\r\n    textViewPrime.setText(String.valueOf(limit) + \"\u306e\u6700\u5927\u7d20\u6570 : \" + String.valueOf(max));\r\n    textViewTime.setText(\"\u51e6\u7406\u6642\u9593 : \" + String.valueOf(endMSec - startMSec) + \"\u30df\u30ea\u79d2\");\r\n}<\/pre>\n<p>(2)\u306e\u8a08\u6e2c\u6642\u9593\u304b\u3089(1)\u306e\u8a08\u6e2c\u6642\u9593\u3092\u5f15\u3044\u3066\u3001\u51e6\u7406\u6642\u9593(ms)\u3092\u753b\u9762\u306b\u8868\u793a\u3057\u307e\u3059\u3002<\/p>\n<p>\u5909\u6570native\u3092\u5207\u308a\u66ff\u3048\u3066C++\u3068Java\u305d\u308c\u305e\u308c\u3067\u8a08\u6e2c\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u7d50\u679c<\/h2>\n<div id=\"attachment_4205\" style=\"width: 586px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-31.382.png\"><img aria-describedby=\"caption-attachment-4205\" loading=\"lazy\" class=\"wp-image-4205 size-large\" src=\"https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-31.382-576x1024.png\" alt=\"\" width=\"576\" height=\"1024\" srcset=\"https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-31.382-576x1024.png 576w, https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-31.382-169x300.png 169w, https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-31.382.png 675w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a><p id=\"caption-attachment-4205\" class=\"wp-caption-text\">\u30cd\u30a4\u30c6\u30a3\u30d6\u5074<\/p><\/div>\n<p>&nbsp;<\/p>\n<div id=\"attachment_4206\" style=\"width: 586px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-33.514.png\"><img aria-describedby=\"caption-attachment-4206\" loading=\"lazy\" class=\"wp-image-4206 size-large\" src=\"https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-33.514-576x1024.png\" alt=\"\" width=\"576\" height=\"1024\" srcset=\"https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-33.514-576x1024.png 576w, https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-33.514-169x300.png 169w, https:\/\/www.sria.co.jp\/blog\/wp-content\/uploads\/2018\/03\/Photo_18-03-14-21-01-33.514.png 675w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a><p id=\"caption-attachment-4206\" class=\"wp-caption-text\">Java\u5074<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>\u30cd\u30a4\u30c6\u30a3\u30d6\u5074\u304c4\u500d\u8fd1\u304f\u65e9\u3044\u7d50\u679c\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u6b63\u76f4\u3001\u30cd\u30a4\u30c6\u30a3\u30d6\u30b3\u30fc\u30c9\u5074\u306fJNI\u547c\u3073\u51fa\u3057\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u3042\u308b\u3068\u601d\u3046\u306e\u3067\u3001\u8efd\u3044\u51e6\u7406\u3067\u306f\u305d\u3053\u307e\u3067\u5909\u308f\u3089\u306a\u3044\u306e\u304b\u306a\u3068\u601d\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u52b9\u679c\u3042\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4eca\u5f8cAndroid\u30a2\u30d7\u30ea\u3067\u901f\u5ea6\u304c\u6c42\u3081\u3089\u308c\u308b\u51e6\u7406\u3092\u66f8\u304f\u969b\u3001\u7a4d\u6975\u7684\u306b\u30cd\u30a4\u30c6\u30a3\u30d6\u30b3\u30fc\u30c9\u3092\u6d3b\u7528\u3057\u3066\u3044\u304d\u307e\u3059\uff01\uff08C\/C++\u597d\u304d\u3060\u3057\uff09<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059\u3002\u958b\u767a\u90e8\u306e\u5e84\u5b50\u3067\u3059\u3002 \u304a\u4ed5\u4e8b\u3067\u521d\u3081\u3066Android NDK\u306b\u89e6\u308c\u308b\u6a5f\u4f1a\u304c\u3042\u308a\u3001C++\u306e\u30cd\u30a4\u30c6\u30a3\u30d6\u30b3\u30fc\u30c9\u304cJava\u30b3\u30fc\u30c9\u306b\u6bd4\u3079\u3066\u3069\u306e\u304f\u3089\u3044\u65e9\u3044\u306e\u304b\u77e5\u308a\u305f\u3044\u3068\u601d\u3063\u3066\u3001\u901f\u5ea6\u6bd4\u8f03\u306e\u5b9f\u9a13\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u305b\u3063\u304b\u304f\u306a&#8230;<\/p>\n","protected":false},"author":6,"featured_media":227,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,8],"tags":[45,173,172,171,170,174],"_links":{"self":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4204"}],"collection":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=4204"}],"version-history":[{"count":13,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4204\/revisions"}],"predecessor-version":[{"id":4219,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4204\/revisions\/4219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=4204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=4204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=4204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}