Created
February 18, 2013 09:50
-
-
Save wangzaixiang/4976290 to your computer and use it in GitHub Desktop.
Cache Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//gcc -DPAD=0 -o test2_0 test2.c -lpthread;time ./test2_0 -- 6.5s | |
//gcc -DPAD=0x0C38 -o test2_0C38 test2.c -lpthread;time ./test2_0C38 -- 15s | |
#include <stdio.h> | |
#include <pthread.h> | |
#include <sys/time.h> | |
// very suprise, when PAD = 0x0C38, the speed is slow down | |
typedef struct _Block | |
{ | |
unsigned char pad1[PAD]; | |
unsigned int flag1; | |
#ifdef CacheLinePadding | |
long int padding[8]; | |
#endif | |
unsigned int flag2; | |
} Block; | |
Block block __attribute__((aligned(0x1000))); | |
unsigned LOOP = 0x80000000; | |
void MyThread(unsigned int* arg) | |
{ | |
struct timeval oStart1, oEnd1; | |
gettimeofday( &oStart1 , NULL ); | |
while( *arg < LOOP) { | |
(*arg)++; | |
} | |
gettimeofday( &oEnd1 , NULL ); | |
printf("arg = %p, used time:%lums\n", arg, (oEnd1.tv_sec-oStart1.tv_sec)*1000 + (oEnd1.tv_usec-oStart1.tv_usec) / 1000); | |
//printf("used time:%lums\n", (oEnd1.tv_sec-oStart1.tv_sec)*1000 + (oEnd1.tv_usec-oStart1.tv_usec) / 1000); | |
} | |
int main(int argc, char **argv) | |
{ | |
pthread_t tid[2]; | |
printf("block = %p size = %d\n", &block, sizeof(block)); | |
block.flag1 = block.flag2 = 0; | |
MyThread( &block.flag1); | |
printf("\nrunning 2 thread now\n"); | |
block.flag1 = block.flag2 = 0; | |
pthread_create( &tid[0], NULL, (void*)MyThread, &block.flag1); | |
pthread_create( &tid[1], NULL, (void*)MyThread, &block.flag2); | |
pthread_join(tid[0], NULL); | |
pthread_join(tid[1], NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment