Mysql 保存float数组的一种方式

最近做人脸识别,想把提取的特征向量,float型的512维数组,保存到Mysql数据库。

借助gson 来实现数据的转换来实现:

        Gson gson = new Gson();
        String str = "[0.046976,-0.035901,-0.023184,0.014815,0.035589,-0.024258,-0.117624,-0.020918,0.041997,-0.019475,-0.035084,0.049479,-0.053332,0.030442,0.000873,0.012008,-0.033603,-0.065246,-0.024993,0.038355,-0.025523,0.011324,-0.002531,0.048163,-0.018388,-0.022137,0.040009,-0.021092,-0.051542,0.070355,0.018515,-0.044039,0.009527,0.032691,-0.030746,0.015813,-0.031645,-0.043081,0.025169,0.107150,0.051683,0.059945,0.015421,-0.027492,0.025517,0.040449,0.015656,-0.044695,-0.023850,-0.022898,0.059189,-0.014924,0.036835,0.028041,-0.004306,-0.112039,-0.046451,0.006685,0.079857,0.089685,0.043670,-0.008110,-0.016577,-0.065293,-0.091973,-0.056071,-0.003158,0.038329,0.035841,-0.031943,-0.101215,-0.027860,-0.040416,0.021554,0.050086,-0.030670,0.063946,0.027227,0.010854,0.054937,0.033359,0.068849,0.033294,-0.090207,-0.027027,-0.012915,0.034793,-0.082008,-0.025265,0.029565,0.041500,0.053676,-0.039105,-0.016145,-0.005789,0.005402,-0.016688,-0.008886,-0.056414,0.055919,-0.005601,0.059420,0.033221,0.001279,-0.075994,0.047679,0.008564,-0.061489,0.009049,0.002608,0.093550,0.000331,-0.037180,0.081949,0.011392,0.041722,-0.038507,0.032674,0.044199,0.051280,-0.007051,-0.008843,0.034946,-0.020169,0.083116,0.029594,-0.041697,0.036578,-0.056620,-0.023083,-0.009278,0.013776,-0.059477,-0.023472,0.010080,0.041776,-0.008270,-0.020077,0.024514,-0.026548,0.041732,0.034395,-0.022222,-0.114354,-0.011827,-0.003673,-0.048816,-0.047096,-0.007047,0.025355,-0.059629,0.001665,-0.013865,0.025291,0.008262,0.056953,0.002059,-0.018192,0.089449,0.017731,-0.053725,0.054384,-0.051639,0.005867,0.030546,-0.034134,-0.001304,-0.033258,-0.006027,-0.033623,0.017016,0.040830,-0.020162,-0.010128,-0.085409,-0.015857,0.004563,0.007276,-0.015291,-0.045935,0.014616,0.031119,0.006249,-0.020381,0.060067,0.073464,0.034771,0.006890,0.005017,0.007413,-0.071259,-0.032291,-0.037167,-0.002957,-0.011262,-0.015170,-0.090247,-0.008788,-0.034878,0.054129,-0.134242,0.028485,-0.004580,-0.048655,-0.040566,0.000724,0.097755,-0.011532,-0.057796,-0.021199,-0.001244,-0.058592,0.071597,0.026428,0.052160,0.102309,0.009825,0.044037,-0.006500,-0.036792,-0.042567,0.031647,0.001352,-0.038041,0.039212,-0.007664,0.027829,-0.071266,-0.052662,0.056408,0.011311,0.016055,-0.024104,0.063310,0.081433,-0.030409,-0.016931,-0.049057,0.083779,0.061561,0.032751,-0.001980,-0.042919,0.123842,0.078794,0.023394,0.077935,-0.063068,-0.006925,0.081333,-0.007835,-0.000680,0.064133,0.008956,0.012096,0.023032,0.019447,-0.074128,0.044642,0.056201,-0.030414,0.016893,-0.015918,-0.011136,-0.031821,-0.023101,0.004474,-0.011158,0.023875,-0.013061,-0.009046,-0.031777,0.008892,-0.014093,0.049141,-0.076293,0.035179,-0.020181,-0.063553,-0.058438,-0.011022,-0.118642,0.075553,0.006787,-0.003762,-0.058338,0.001187,-0.050501,-0.003424,-0.007157,0.009644,0.058500,-0.058442,-0.018812,-0.007002,-0.048304,-0.026082,0.039158,0.001202,-0.035852,-0.008896,0.091594,0.010801,0.012007,0.022367,0.007064,-0.062576,0.032535,-0.037729,0.002838,0.009103,-0.031105,-0.032393,0.003926,-0.060115,-0.044278,-0.029738,-0.056600,0.010172,0.094547,-0.009931,-0.026345,0.015897,0.006202,0.013937,0.004103,0.088686,-0.017466,-0.031264,0.060829,0.000377,0.011637,-0.028611,-0.016244,-0.002182,-0.096342,0.006462,0.084697,-0.048682,-0.034335,-0.062791,-0.037432,0.022813,-0.019076,0.082471,0.008379,0.036276,-0.021859,-0.061209,0.042735,-0.047293,0.013335,0.003810,-0.005651,0.002799,0.045750,-0.018697,0.052684,-0.042938,-0.045899,-0.011187,0.000529,-0.004732,-0.012763,-0.036971,-0.006564,0.026557,0.038586,0.004211,0.046067,-0.062571,0.035374,0.076194,0.012479,-0.009211,0.010909,0.019407,-0.040816,0.033278,-0.015997,-0.029139,0.004380,0.130317,-0.005323,0.060276,0.008486,0.034945,0.026428,-0.117780,-0.055746,0.021376,0.011579,0.044952,0.060421,-0.045017,0.012504,-0.025474,-0.007238,0.048930,-0.075786,0.050105,0.015769,0.061967,0.054163,0.025279,0.047959,0.006276,0.051834,-0.041330,0.010510,-0.000701,-0.052760,-0.016573,-0.081074,0.018058,0.029957,0.042522,-0.019366,-0.005891,-0.026318,-0.021674,0.046437,-0.027693,-0.037408,0.024347,-0.010308,-0.115526,0.038141,-0.046006,-0.024414,-0.022456,-0.020808,0.083297,-0.049632,-0.057566,0.060836,0.024246,-0.057238,-0.024468,-0.053545,0.005358,0.020657,-0.024109,0.003247,-0.012480,-0.054809,-0.016033,-0.068541,0.026738,-0.027674,-0.001279,0.059598,0.018346,-0.043920,-0.064533,-0.031084,0.035836,-0.054980,0.037794,-0.026936,0.029582,0.017165,-0.028804,-0.028711,-0.026829,0.019000,0.031892,0.008277,0.037088,0.099696,0.050406,0.063010,-0.037109,0.035078,-0.030225,-0.028696,0.039772,0.026353,0.025733,-0.032334,-0.090609,-0.000840,0.052345,0.010931,0.080128,-0.010244,-0.109285,0.018599,0.071492,-0.039366,0.057105,0.027364,0.001542,-0.084782,-0.035863,0.092739,-0.009981,0.024515,0.018256,-0.082417,0.058568,0.023865,-0.058869,-0.030154,-0.010006,-0.040866,-0.055976,0.014211,0.052039,0.033550,0.040604,0.058277]";
        // str 转float数组
        Float[] b = gson.fromJson(str, Float[].class);
        Float[] a = gson.fromJson(str, Float[].class);

        long beginTimer =    System.currentTimeMillis();
        for (int j =0;j<10000;j++){
            float score = 0.0f;
            for (int i =0;i<512;i++){
                score += a[i]*b[i];
            }
            float bb = (score +1.0f)*0.5f;
        }
       //1.float[]数组转换为json
		String str1 = gson.toJson(a);

mysql 的存储使用text 字段保存。

猜你喜欢

转载自blog.csdn.net/houxian1103/article/details/114086772