您的位置:首页 > 运维架构

blake2与md5算法速度对比测试 使用OpenMP优化

2015-11-22 18:48 507 查看
/* Test case for blake2s blake2sp blake2sp_file vs md5 md5_parallel
* Result: md5_parallel is fastest.
* MD5_File() and MD5_File_Parallel(), see https://github.com/9468305/crsync/blob/master/extra/md5.h */
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>

#include "blake2.h"
#include "md5.h"

#define buflen 8*1024

int tests(const char *filename) {
struct timeb start, end;
ftime(&start);
FILE *file = NULL;

if ( (file = fopen(filename, "rb")) == NULL ) {
return -1;
}

uint8_t *buf = malloc(buflen);
uint8_t sum[BLAKE2S_OUTBYTES];
blake2s_state S;
blake2s_init( &S, BLAKE2S_OUTBYTES );
size_t length = 0;
while( 0 != (length = fread(buf, 1, buflen, file)) ) {
blake2s_update( &S, ( const uint8_t * )buf, length );
}
blake2s_final( &S, sum, BLAKE2S_OUTBYTES );

ftime(&end);
double time = (1000.0 * difftime(end.time, start.time) + end.millitm - start.millitm) / 1000;
printf("blake2s time = %f seconds\n", time);
for(int i=0; i<BLAKE2S_OUTBYTES; i++)
printf("%02x", sum[i]);
printf("\n");
free(buf);
fclose(file);
return 0;
}

int testsp(const char *filename) {
struct timeb start, end;
ftime(&start);
FILE *file = NULL;

if ( (file = fopen(filename, "rb")) == NULL ) {
return -1;
}

uint8_t *buf = malloc(buflen);
uint8_t sum[BLAKE2S_OUTBYTES];
blake2sp_state S;
blake2sp_init( &S, BLAKE2S_OUTBYTES );
size_t length = 0;
while( 0 != (length = fread(buf, 1, buflen, file)) ) {
blake2sp_update( &S, ( const uint8_t * )buf, length );
}
blake2sp_final( &S, sum, BLAKE2S_OUTBYTES );

ftime(&end);
double time = (1000.0 * difftime(end.time, start.time) + end.millitm - start.millitm) / 1000;
printf("blake2sp time = %f seconds\n", time);
for(int i=0; i<BLAKE2S_OUTBYTES; i++)
printf("%02x", sum[i]);
printf("\n");
free(buf);
fclose(file);
return 0;
}

void testblake2sp_file(const char *filename) {
struct timeb start, end;
ftime(&start);
uint8_t sum[BLAKE2S_OUTBYTES];
blake2sp_file(filename, sum, BLAKE2S_OUTBYTES);
ftime(&end);
double time = (1000.0 * difftime(end.time, start.time) + end.millitm - start.millitm) / 1000;
printf("blake2sp_file time = %f seconds\n", time);
for(int i=0; i<BLAKE2S_OUTBYTES; i++)
printf("%02x", sum[i]);
printf("\n");
}

void testMD5(const char *filename, int isParallel) {
struct timeb start, end;
ftime(&start);
uint8_t sum[MD5_OUTBYTES];
if(isParallel == 0)
MD5_File(filename, sum);
else
MD5_File_Parallel(filename, sum);
ftime(&end);
double time = (1000.0 * difftime(end.time, start.time) + end.millitm - start.millitm) / 1000;
if(isParallel == 0)
printf("md5 time = %f seconds\n", time);
else
printf("md5p time = %f seconds\n", time);
for (int j = 0; j < MD5_OUTBYTES; j++)
printf("%02x", sum[j]);
printf("\n");
}

int main( int argc, char **argv ) {
const char *filename = "/sdcard/test.obb";
tests(filename);
//testsp(filename);
testblake2sp_file(filename);
testMD5(filename, 0);
testMD5(filename, 1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: