字符串异或比较

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

#define MAX_SIZE 1024*1024

int 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

 

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

转载于:https://www.cnblogs.com/mtsyl/p/5809123.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值