#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
时间: 2024-11-06 09:53:53