首页 > 代码库 > 字符串异或比较

字符串异或比较

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <sys/time.h>#include <unistd.h>#define MAX_SIZE 1024*1024int main(int argc, char **argv){    unsigned char *a = NULL;    unsigned char *b = NULL;    unsigned char *c = NULL;    struct timeval start, end;    int i = 0, tmp = 0;    unsigned long long x = 0, y = 0, z = 0;#if 0    unsigned int *g;    unsigned char h[16]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x11,0x22,0x33,0x44,0x55,0x66,0x77};    g = (unsigned int *)h;    printf("#################### %08x %08x\n", *g, *g);    g++;    printf("#################### %08x %08x\n", *g, *g);#endif        a = (unsigned char *)malloc(MAX_SIZE);    b = (unsigned char *)malloc(MAX_SIZE);    c = (unsigned char *)malloc(MAX_SIZE);    memset(a, 0x65, MAX_SIZE);    memset(b, 0x46, MAX_SIZE);    gettimeofday(&start, NULL);    for (i = 0; i < MAX_SIZE; i++)    {        c[i] = a[i] ^ b[i];    }    gettimeofday(&end, NULL);    printf("[%d]take %dus \n", __LINE__, (int)(((end.tv_sec-start.tv_sec)*1000000+end.tv_usec)-start.tv_usec));    printf("######################\n");    for (i = 0; i < 64; i++)    {        if ((i%16) == 0 && i != 0)            printf("\n");        printf("%02x ", c[i]);    }    printf("\n");    gettimeofday(&start, NULL);    tmp = MAX_SIZE / 8;    for (i = 0; i < tmp; i++)    {        memcpy((char *)&x, a+i*8, 8);        memcpy((char *)&y, b+i*8, 8);        z = x ^ y;        memcpy(c+i*8, (char *)&z, 8);    }    gettimeofday(&end, NULL);    printf("[%d]take %dus \n", __LINE__, (int)(((end.tv_sec-start.tv_sec)*1000000+end.tv_usec)-start.tv_usec));    printf("######################\n");    for (i = 0; i < 64; i++)    {        if ((i%16) == 0 && i != 0)            printf("\n");        printf("%02x ", c[i]);    }    printf("\n");    unsigned long long *l = (unsigned long long *)a;    unsigned long long *m = (unsigned long long *)b;    unsigned long long *n = (unsigned long long *)c;    tmp = MAX_SIZE / 8;    gettimeofday(&start, NULL);    for (i = 0; i < tmp; i++)    {        *n = *l ^ *m;        l++;        m++;        n++;    }    gettimeofday(&end, NULL);    printf("[%d]take %dus \n", __LINE__, (int)(((end.tv_sec-start.tv_sec)*1000000+end.tv_usec)-start.tv_usec));    printf("######################\n");    for (i = 0; i < 64; i++)    {        if ((i%16) == 0 && i != 0)            printf("\n");        printf("%02x ", c[i]);    }    printf("\n");    return 0;}

[40]take 14714us
######################
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
[61]take 2687us
######################
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
[85]take 1958us
######################
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23

 

结果显示,使用指针速度最快

字符串异或比较