Skip to content

Instantly share code, notes, and snippets.

@9468305
Last active December 16, 2015 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9468305/97dca7c470ee02a6867c to your computer and use it in GitHub Desktop.
Save 9468305/97dca7c470ee02a6867c to your computer and use it in GitHub Desktop.
blake2与md5算法速度对比测试 包括OpenMP并行加速优化
/* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment