java端:java
以毫秒爲單位 long startTime = System.currentTimeMillis(); // 獲取開始時間 // doThing(); // 測試的代碼段 long endTime = System.currentTimeMillis(); // 獲取結束時間 Log.e("wsy","代碼運行時間: " + (endTime - startTime) + "ms"); 1 2 3 4 以納秒爲單位 long startTime = System.nanoTime(); // 獲取開始時間 // doThing(); // 測試的代碼段 long endTime = System.nanoTime(); // 獲取結束時間 Log.e("wsy","代碼運行時間: " + (endTime - startTime) + "ms");
jni,好像打印不出來,須要log.e測試
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "caltime_Jni.h" JNIEXPORT jlong JNICALL Java_caltime_Jni_calculateTime (JNIEnv *e, jobject jo){ long start, end; int i,j,k; long a; //start time start = clock(); //do something for (i = 0; i < 14400000; i++) { a = 255.0 / 16.0; } //end time end = clock(); return end - start; } JNIEXPORT jstring JNICALL Java_caltime_Jni_printStr (JNIEnv *e, jobject j) { return (*e)->NewStringUTF(e, "Rambow"); }