码迷,mamicode.com
首页 > 其他好文 > 详细

字符串做异或使用union

时间:2016-08-15 14:25:10      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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

union data
{
    unsigned char str[2048];
    unsigned long long ll_value[256];
    unsigned int i_value[512];
};

int main(int argc, char **argv)
{
    union data a;
    union data b;
    struct timeval start, end;
    int i = 0;
    
    memset((char *)&a, 1, 2048);
    memset((char *)&b, 1, 2048);

    gettimeofday(&start, NULL);
    for (i = 0; i < 2048; i++)
    {
        a.str[i] = a.str[i] ^ b.str[i];
    }
    gettimeofday(&end, NULL);
    printf("take %dus \n", (int)(((end.tv_sec-start.tv_sec)*1000000+end.tv_usec)-start.tv_usec));

    gettimeofday(&start, NULL);
    for (i = 0; i < 256; i++)
    {
        a.ll_value[i] = a.ll_value[i] ^ b.ll_value[i];
    }
    gettimeofday(&end, NULL);
    printf("take %dus \n", (int)(((end.tv_sec-start.tv_sec)*1000000+end.tv_usec)-start.tv_usec));

    gettimeofday(&start, NULL);
    for (i = 0; i < 512; i++)
    {
        a.i_value[i] = a.i_value[i] ^ b.i_value[i];
    }
    gettimeofday(&end, NULL);
    printf("take %dus \n", (int)(((end.tv_sec-start.tv_sec)*1000000+end.tv_usec)-start.tv_usec));

  return 0; }

take 21us
take 3us
take 5us

字符串做异或使用union

标签:

原文地址:http://www.cnblogs.com/mtsyl/p/5772557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!