cpu内存加载行size为64字节
参考代码:
#include <stdio.h>
#include <pthread.h>
static long COUNT = 1000000000;
typedef struct
{
// long tmp1[7];
long value;
// long tmp2[7];
}NODE;
NODE data[2];
void *my_add_func(void *args){
NODE *mydata = (NODE *)args;
for(long i =0;i < COUNT;i++){
mydata->value = i;
}
return NULL;
}
int main(){
int thread_num = 2;
pthread_t pt[thread_num];
for(int i = 0; i < thread_num;i++){
pthread_create(&pt[i], NULL, my_add_func, (void *)&data[i]);
}
for(int i = 0; i < thread_num;i++){
pthread_join(pt[i], NULL);
}
return 0;
}
验证结果:
cpu缓存一致对系统的性能确实有比较大的影响,在极致性能优化方面可以考虑
# 打开 tmp 定义
C02CN0QPMD6V:$ gcc cache.c
C02CN0QPMD6V:$ time ./a.out
real 0m2.926s
user 0m4.488s
sys 0m0.011s
# 关闭 tmp 定义
C02CN0QPMD6V:$ gcc cache.c
C02CN0QPMD6V $ time ./a.out
real 0m5.321s
user 0m9.952s
sys 0m0.015s