문제

Here is my program in C.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

static int DATA[1024]={1,2,3,4,.....1024};

inline void foo_0(void)
{
    int j;
    puts("Hello, I'm inside foo_0");
    int k=0;
    for(j=0;j<1024;j++)
    {
        //k=DATA[j];
        DATA[j]+=1;
    }
    k+=0;
}


inline void foo_1(void)
{
    int j;
    puts("Hello, I'm inside foo_1");
    int k=0;
    for(j=0;j<1024;j+=4)
    {
        //k=DATA[j];
        DATA[j]+=1;
    }
    k+=0;
}

inline void foo_2(void)
{
    int j;
    puts("Hello, I'm inside foo_2");
    int k=0;
    for(j=0;j<1024;j+=16)
    {
        //k=DATA[j];
        DATA[j]+=1;
    }
    k+=0;
}


inline uint64_t rdtsc()
{
    unsigned long a, d;
    asm volatile ("cpuid; rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx");// core i3/i7 
    return a | ((uint64_t)d << 32);
}

inline void clflush(volatile void *p)
{
    asm volatile ("clflush (%0)" :: "r"(p));
}

int main(void)
{

    volatile uint64_t start, end,temp;
    unsigned long long total_time=0;


    foo_0();  // This will load DATA array into memory
    foo_1();  // DATA already loaded into memory
    foo_2(); // DATA already loaded into memory

    printf("**********************\n");  
    start=rdtsc();
    foo_2();
    end=rdtsc();
    temp=end-start;
    total_time=temp;

    printf("Time taken foo_2 = %llu\n",total_time);             

    start=rdtsc();
    foo_1();
    end=rdtsc();
    temp=end-start;
    total_time=temp;

    printf("Time taken foo_1 = %llu\n",total_time);

    start=rdtsc();
    foo_0();
    end=rdtsc();
    temp=end-start;
    total_time=temp;

    printf("Time taken foo_0 = %llu\n",total_time);


    return 0;
}

output :

Hello, I'm inside foo_0
Hello, I'm inside foo_1
Hello, I'm inside foo_2
**********************
Hello, I'm inside foo_2
**Time taken foo_2 = 6350**
Hello, I'm inside foo_1
**Time taken foo_1 = 10056**
Hello, I'm inside foo_0
**Time taken foo_0 = 21726**

EDIT 1 :

When I remove puts() from all three functions , I got this result

Time taken foo_0 = 16448
Time taken foo_1 = 4438
**********************
Time taken foo_2 = 1548

I am expecting same access time for all three foo_0, foo_1 and foo_2 as when memory is accessed to access an integer a whole cache block is loaded (64 bytes is block size, so 16 integers are loaded inside cache block) , so access time for 1, 4, 16 integer must be same. I am not getting that. Where I am making mistake ? I am using gcc under linux. Thanks in advance .

EDIT 2

As per answer by BЈовић and suggestion of Leeor in comment, the execution time must be different as there are different no of addition operations performed in three different functions. Although there are same no of misses in all three cases, but no of execution of addition operation varies and for all operations data is accessed from cache(*not from main memory* as I suspect) the total time depends on no of addition operation performed on array, not on cache miss/hit. So I accept the answer of BЈовић and suggestion of Leeor. Thank you to all of you.

도움이 되었습니까?

해결책

1) Your process is being pushed to the background by other processes, therefore it is not possible to get precise timings.

2) The times can not be possible be the same because of different loops:

foo_0() : for(j=0;j<1024;j++)
foo_1() : for(j=0;j<1024;j+=4)
foo_2() : for(j=0;j<1024;j+=16)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top