c++浮点运算能力附安卓版

Home / C++ 百晓生 2019-3-14 4522

时代在发展,科技在进步。CPU处理能力越来越强,小白一般是用CPU-Z或者鲁大师之类的软件看跑分,下面我也给出一个小程序,更简单明了的体现你电脑的CPU运算能力。时间越短越好。代码为Linux版本和Android版,Windows版本需要自己稍微改一下下。

Linux

#include <string>
#include <strings.h>
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <pthread.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <sys/select.h>
#include <termios.h>
#include<sys/ioctl.h>
#include <math.h>
#include <assert.h>
typedef unsigned int DWORD;
int kbhit() {
    static const int STDIN = 0;
    static bool initialized = false;
    if (!initialized) {
        // Use termios to turn off line buffering
        termios term;
        tcgetattr(STDIN, &term);
        term.c_lflag &= ~ICANON;
        tcsetattr(STDIN, TCSANOW, &term);
        setbuf(stdin, NULL);
        initialized = true;
    }
    int bytesWaiting;
    ioctl(STDIN, FIONREAD, &bytesWaiting);
    return bytesWaiting;
}
int getch() {
    struct termios oldt, newt;
    int ch;
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    ch = getchar();
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    return ch;
}
//#define DWORD unsigned int  volatile
DWORD GetTick(void) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (tv.tv_sec * 1000L + tv.tv_usec / 1000);
}
double logval(double x) {
    return log(x + sqrt(1 + x * x));
}
double GetY(double x) {
    return (x * sqrt(1 + x * x) + log(x + sqrt(1 + x * x)));
}
double GetX(double y, int s = 0);
double GetX(double y, int s) {
    int iCount = 0;
    double x = sqrt(2) * y * sqrt(1 / (sqrt(4 * y * y + 1) + 1));
    double fXLastStart = 0, fXLastEnd = x, fXMiddle = (fXLastEnd + fXLastStart) / 2;
    double fYMiddle = 0;
    do {
        fYMiddle = GetY(fXMiddle);
        if (fYMiddle > y) {
            fXLastEnd = fXMiddle;
            fXMiddle = (fXLastStart + fXMiddle) / 2;
        } else {
            fXLastStart = fXMiddle;
            fXMiddle = (fXLastEnd + fXMiddle) / 2;
        }
        iCount++;
    } while (fabs(fYMiddle - y) > 0.0001);
    if (s)printf("%d\n", iCount);
    return fXMiddle;
}
int main() {
    volatile int id = 0x100;
    volatile double x, y;
    x = 100.0;
    y = 100.0;
    volatile double a = atan2(y, x), b = 0, c = 0, d = 0, ww = 0;
    volatile DWORD t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = atan2(y, x + 1 + i / 1000.0);
        b = sin(x / 10.0 + i / 1000.01);
        c = cos(y / 100.0 + i / 1000.02);
        d = sqrt(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = atan2(y, x + 10 + i / 1000.04);
        b = sin(x / 10.01 + i / 1000.05);
        c = cos(y / 100.01 + i / 1000.06);
        d = sqrt(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = atan2(y, x + 11 + i / 1000.08);
        b = sin(x / 10.02 + i / 1000.09);
        c = cos(y / 100.02 + i / 1000.10);
        d = sqrt(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = atan2(y, x + 12 + i / 1000.120);
        b = sin(x / 10.03 + i / 1000.130);
        c = cos(y / 100.03 + i / 1000.140);
        d = sqrt(y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    volatile DWORD t1 = GetTick();
    printf("----------\n");
    char msg[512] = {0};
    printf("ID=%d atan2=%f\n time=%d(ms), zhys(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    printf(msg, "ID=%d atan2=%f\n time=%d(ms), zhys(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = atan2(y, x + 1 + i / 1000.0);
        b = atan2(y, x / 10.0 + i / 1000.01);
        c = atan2(x, y / 100.0 + i / 1000.02);
        d = atan2(y, y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = atan2(y, x + 10 + i / 1000.04);
        b = atan2(y, x / 10.01 + i / 1000.05);
        c = atan2(x, y / 100.01 + i / 1000.06);
        d = atan2(x, y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = atan2(y, x + 11 + i / 1000.08);
        b = atan2(y, x / 10.02 + i / 1000.09);
        c = atan2(x, y / 100.02 + i / 1000.10);
        d = atan2(x, y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = atan2(y, x + 12 + i / 1000.120);
        b = atan2(y, x / 10.03 + i / 1000.130);
        c = atan2(x, y / 100.03 + i / 1000.140);
        d = atan2(x, y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    printf("ID=%d atan2=%f\n time=%d(ms), atan2(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    bzero(msg, 512);
    printf(msg, "ID=%d atan2=%f\n time=%d(ms), atan2(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    t0 = GetTick();
    for (volatile int i = 0; i < 100 * 1000L; i++) {
        a = GetX(x + 1 + i / 1000.0);
        b = GetX(x / 10.0 + i / 1000.01);
        c = GetX(y / 100.0 + i / 1000.02);
        d = GetX(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = GetX(x + 10 + i / 1000.04);
        b = GetX(x / 10.01 + i / 1000.05);
        c = GetX(y / 100.01 + i / 1000.06);
        d = GetX(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = GetX(x + 11 + i / 1000.08);
        b = GetX(x / 10.02 + i / 1000.09);
        c = GetX(y / 100.02 + i / 1000.10);
        d = GetX(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = GetX(x + 12 + i / 1000.120);
        b = GetX(x / 10.03 + i / 1000.130);
        c = GetX(y / 100.03 + i / 1000.140);
        d = GetX(i * 1);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    bzero(msg, 512);
    printf("ID=%d FlatMov=%f\n time=%d(ms), FlatMov(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 1600.0);
    printf(msg, "ID=%d FlatMov=%f\n time=%d(ms), FlatMov(time)=%.3f(us)\n", id,
            b, t1 - t0,
            (t1 - t0) / 1600.0);
    t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = asinh(x + 1 + i / 1000.0);
        b = asinh(x / 10.0 + i / 1000.01);
        c = asinh(y / 100.0 + i / 1000.02);
        d = asinh(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = asinh(x + 10 + i / 1000.04);
        b = asinh(x / 10.01 + i / 1000.05);
        c = asinh(y / 100.01 + i / 1000.06);
        d = asinh(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = asinh(x + 11 + i / 1000.08);
        b = asinh(x / 10.02 + i / 1000.09);
        c = asinh(y / 100.02 + i / 1000.10);
        d = asinh(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = asinh(x + 12 + i / 1000.120);
        b = asinh(x / 10.03 + i / 1000.130);
        c = asinh(y / 100.03 + i / 1000.140);
        d = asinh(y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    printf("ID=%d asinh=%f\n time=%d(ms), asinh(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    bzero(msg, 512);
    printf(msg, "ID=%d asinh=%f\n time=%d(ms), asinh(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = logval(x + 1 + i / 1000.0);
        b = logval(x / 10.0 + i / 1000.01);
        c = logval(y / 100.0 + i / 1000.02);
        d = logval(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = logval(x + 10 + i / 1000.04);
        b = logval(x / 10.01 + i / 1000.05);
        c = logval(y / 100.01 + i / 1000.06);
        d = logval(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = logval(x + 11 + i / 1000.08);
        b = logval(x / 10.02 + i / 1000.09);
        c = logval(y / 100.02 + i / 1000.10);
        d = logval(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = logval(x + 12 + i / 1000.120);
        b = logval(x / 10.03 + i / 1000.130);
        c = logval(y / 100.03 + i / 1000.140);
        d = logval(y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    bzero(msg, 512);
    printf("ID=%d logval=%f\n time=%d(ms), logval(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    printf(msg, "ID=%d logval=%f\n time=%d(ms), logval(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    printf("---------\n");
    bzero(msg, 512);
    printf(msg, "-----线程:%d测试结束-----\n", id);
    return 0;
}

Android版本

#include <jni.h>
#include <string>
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <pthread.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <sys/select.h>
#include <termios.h>
#include<sys/ioctl.h>
#include <math.h>
#include <assert.h>
#include <android/log.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "fjsmthlib-log", __VA_ARGS__))
typedef unsigned int DWORD;
void JNI_Callback(char *pData, int id); //回调函数
static JavaVM *g_Jvm = NULL;
static jobject g_obj = NULL;
extern "C"
JNIEXPORT jstring JNICALL
Java_test_math_com_mathtest_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject obj/* this */) {
    g_obj = env->NewGlobalRef(obj);//创建全局对象用于回调函数使用
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env = NULL;
    jint result = -1;
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        LOGI("GetEnv Failed", g_Jvm);
        return -1;
    }
    assert(env != NULL);
    /* success -- return valid version number */
    result = JNI_VERSION_1_6;
    g_Jvm = vm;
    LOGI("jvm:%08X", g_Jvm);
    return result;
}
void JNI_Callback(char *pData, int id) //回调函数
{
    JNIEnv *pEnv = NULL;
    //Attach
    if (g_Jvm->AttachCurrentThread(&pEnv, NULL) == JNI_OK) {
        LOGI("%08X=>%08X=>%08X", g_Jvm, g_obj, pEnv);
        jclass Classlib = pEnv->GetObjectClass(g_obj);
        jobject Objectlib = pEnv->NewLocalRef(g_obj);
        jmethodID CallBack = pEnv->GetMethodID(Classlib, "onDataCallbk", "(Ljava/lang/String;I)V");
        jstring text = pEnv->NewStringUTF(pData);
        pEnv->CallVoidMethod(Objectlib, CallBack, text, id);
        pEnv->DeleteLocalRef(text);
        pEnv->DeleteLocalRef(Objectlib);
        pEnv->DeleteLocalRef(Classlib);
        //Detach
        g_Jvm->DetachCurrentThread();
    }
}
int kbhit() {
    static const int STDIN = 0;
    static bool initialized = false;
    if (!initialized) {
        // Use termios to turn off line buffering
        termios term;
        tcgetattr(STDIN, &term);
        term.c_lflag &= ~ICANON;
        tcsetattr(STDIN, TCSANOW, &term);
        setbuf(stdin, NULL);
        initialized = true;
    }
    int bytesWaiting;
    ioctl(STDIN, FIONREAD, &bytesWaiting);
    return bytesWaiting;
}
int getch() {
    struct termios oldt, newt;
    int ch;
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    ch = getchar();
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    return ch;
}
//#define DWORD unsigned int  volatile
DWORD GetTick(void) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (tv.tv_sec * 1000L + tv.tv_usec / 1000);
}
double logval(double x) {
    return log(x + sqrt(1 + x * x));
}
double GetY(double x) {
    return (x * sqrt(1 + x * x) + log(x + sqrt(1 + x * x)));
}
double GetX(double y, int s = 0);
double GetX(double y, int s) {
    int iCount = 0;
    double x = sqrt(2) * y * sqrt(1 / (sqrt(4 * y * y + 1) + 1));
    double fXLastStart = 0, fXLastEnd = x, fXMiddle = (fXLastEnd + fXLastStart) / 2;
    double fYMiddle = 0;
    do {
        fYMiddle = GetY(fXMiddle);
        if (fYMiddle > y) {
            fXLastEnd = fXMiddle;
            fXMiddle = (fXLastStart + fXMiddle) / 2;
        } else {
            fXLastStart = fXMiddle;
            fXMiddle = (fXLastEnd + fXMiddle) / 2;
        }
        iCount++;
    } while (fabs(fYMiddle - y) > 0.0001);
    if (s)printf("%d\n", iCount);
    return fXMiddle;
}
void *testMath(void *data) {
    volatile int id = *((int *) data);
    volatile double x, y;
    x = 100.0;
    y = 100.0;
    volatile double a = atan2(y, x), b = 0, c = 0, d = 0, ww = 0;
    volatile DWORD t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = atan2(y, x + 1 + i / 1000.0);
        b = sin(x / 10.0 + i / 1000.01);
        c = cos(y / 100.0 + i / 1000.02);
        d = sqrt(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = atan2(y, x + 10 + i / 1000.04);
        b = sin(x / 10.01 + i / 1000.05);
        c = cos(y / 100.01 + i / 1000.06);
        d = sqrt(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = atan2(y, x + 11 + i / 1000.08);
        b = sin(x / 10.02 + i / 1000.09);
        c = cos(y / 100.02 + i / 1000.10);
        d = sqrt(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = atan2(y, x + 12 + i / 1000.120);
        b = sin(x / 10.03 + i / 1000.130);
        c = cos(y / 100.03 + i / 1000.140);
        d = sqrt(y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    volatile DWORD t1 = GetTick();
    LOGI("----------\n");
    char msg[512] = {0};
    LOGI("ID=%d atan2=%f\n time=%d(ms), zhys(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    sprintf(msg, "ID=%d atan2=%f\n time=%d(ms), zhys(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    JNI_Callback(msg, id);
    t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = atan2(y, x + 1 + i / 1000.0);
        b = atan2(y, x / 10.0 + i / 1000.01);
        c = atan2(x, y / 100.0 + i / 1000.02);
        d = atan2(y, y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = atan2(y, x + 10 + i / 1000.04);
        b = atan2(y, x / 10.01 + i / 1000.05);
        c = atan2(x, y / 100.01 + i / 1000.06);
        d = atan2(x, y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = atan2(y, x + 11 + i / 1000.08);
        b = atan2(y, x / 10.02 + i / 1000.09);
        c = atan2(x, y / 100.02 + i / 1000.10);
        d = atan2(x, y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = atan2(y, x + 12 + i / 1000.120);
        b = atan2(y, x / 10.03 + i / 1000.130);
        c = atan2(x, y / 100.03 + i / 1000.140);
        d = atan2(x, y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    LOGI("ID=%d atan2=%f\n time=%d(ms), atan2(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    memset(msg, 0, 512);
    sprintf(msg, "ID=%d atan2=%f\n time=%d(ms), atan2(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    JNI_Callback(msg, id);
    t0 = GetTick();
    for (volatile int i = 0; i < 100 * 1000L; i++) {
        a = GetX(x + 1 + i / 1000.0);
        b = GetX(x / 10.0 + i / 1000.01);
        c = GetX(y / 100.0 + i / 1000.02);
        d = GetX(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = GetX(x + 10 + i / 1000.04);
        b = GetX(x / 10.01 + i / 1000.05);
        c = GetX(y / 100.01 + i / 1000.06);
        d = GetX(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = GetX(x + 11 + i / 1000.08);
        b = GetX(x / 10.02 + i / 1000.09);
        c = GetX(y / 100.02 + i / 1000.10);
        d = GetX(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = GetX(x + 12 + i / 1000.120);
        b = GetX(x / 10.03 + i / 1000.130);
        c = GetX(y / 100.03 + i / 1000.140);
        d = GetX(i * 1);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    memset(msg, 0, 512);
    LOGI("ID=%d FlatMov=%f\n time=%d(ms), FlatMov(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 1600.0);
    sprintf(msg, "ID=%d FlatMov=%f\n time=%d(ms), FlatMov(time)=%.3f(us)\n", id,
            b, t1 - t0,
            (t1 - t0) / 1600.0);
    JNI_Callback(msg, id);
    t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = asinh(x + 1 + i / 1000.0);
        b = asinh(x / 10.0 + i / 1000.01);
        c = asinh(y / 100.0 + i / 1000.02);
        d = asinh(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = asinh(x + 10 + i / 1000.04);
        b = asinh(x / 10.01 + i / 1000.05);
        c = asinh(y / 100.01 + i / 1000.06);
        d = asinh(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = asinh(x + 11 + i / 1000.08);
        b = asinh(x / 10.02 + i / 1000.09);
        c = asinh(y / 100.02 + i / 1000.10);
        d = asinh(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = asinh(x + 12 + i / 1000.120);
        b = asinh(x / 10.03 + i / 1000.130);
        c = asinh(y / 100.03 + i / 1000.140);
        d = asinh(y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    LOGI("ID=%d asinh=%f\n time=%d(ms), asinh(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    memset(msg, 0, 512);
    sprintf(msg, "ID=%d asinh=%f\n time=%d(ms), asinh(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    JNI_Callback(msg, id);
    t0 = GetTick();
    for (volatile int i = 0; i < 1000 * 1000L; i++) {
        a = logval(x + 1 + i / 1000.0);
        b = logval(x / 10.0 + i / 1000.01);
        c = logval(y / 100.0 + i / 1000.02);
        d = logval(y / x * 10 + i / 1000.03);
        ww = a + b + c + d;
        a = logval(x + 10 + i / 1000.04);
        b = logval(x / 10.01 + i / 1000.05);
        c = logval(y / 100.01 + i / 1000.06);
        d = logval(y / x * 101 + i / 1000.07);
        a = ww - c + a + b + c + d;
        ww = a + ww;
        a = logval(x + 11 + i / 1000.08);
        b = logval(x / 10.02 + i / 1000.09);
        c = logval(y / 100.02 + i / 1000.10);
        d = logval(y / x * 102 + i / 1000.110);
        ww = a + b + c + d;
        a = logval(x + 12 + i / 1000.120);
        b = logval(x / 10.03 + i / 1000.130);
        c = logval(y / 100.03 + i / 1000.140);
        d = logval(y / x * 103 + i / 1000.150);
        b = ww - c + a + b + c + d;
        ww = a + ww;
    }
    t1 = GetTick();
    memset(msg, 0, 512);
    LOGI("ID=%d logval=%f\n time=%d(ms), logval(time)=%.3f(us)\n", id, b, t1 - t0,
         (t1 - t0) / 16000.0);
    sprintf(msg, "ID=%d logval=%f\n time=%d(ms), logval(time)=%.3f(us)\n", id, b,
            t1 - t0,
            (t1 - t0) / 16000.0);
    JNI_Callback(msg, id);
    LOGI("---------\n");
    memset(msg, 0, 512);
    sprintf(msg, "-----线程:%d测试结束-----\n", id);
    JNI_Callback(msg, id);
    return NULL;
}
extern "C" JNIEXPORT void JNICALL
Java_test_math_com_mathtest_MainActivity_convert(JNIEnv *env, jobject obj, jbyteArray data) {
    int outLen = env->GetArrayLength(data);
    char json[512] = {0};
    env->SetByteArrayRegion(data, 0, outLen, (jbyte *) json);
}
extern "C" JNIEXPORT void JNICALL
Java_test_math_com_mathtest_MainActivity_test(JNIEnv *env, jobject obj, jint index) {
    //testMath((void*)&id[0]);
    static int id[4];
    id[0] = 1;
    id[1] = 2;
    id[2] = 3;
    id[3] = 4;
    pthread_t thread_ID;
    pthread_create(&thread_ID, NULL, (void *(*)(void *)) testMath, (void *) &id[0]);
    switch (index) {
        case 2:
            //id = 2;
            pthread_create(&thread_ID, NULL, (void *(*)(void *)) testMath, (void *) &id[1]);
            break;
        case 3:
            //id = 2;
            pthread_create(&thread_ID, NULL, (void *(*)(void *)) testMath, (void *) &id[1]);
            //id = 3;
            pthread_create(&thread_ID, NULL, (void *(*)(void *)) testMath, (void *) &id[2]);
            break;
        case 4:
            //id = 2;
            pthread_create(&thread_ID, NULL, (void *(*)(void *)) testMath, (void *) &id[1]);
            //id = 3;
            pthread_create(&thread_ID, NULL, (void *(*)(void *)) testMath, (void *) &id[2]);
            //id = 4;
            pthread_create(&thread_ID, NULL, (void *(*)(void *)) testMath, (void *) &id[3]);
            break;
    }
}


本文链接:https://www.it72.com/12510.htm

推荐阅读
最新回复 (1)
返回