简体   繁体   中英

Should be slower, but is faster. Why?

Ok, so let me explain a little bit about this. In array SL[] i got pointer to lists ( I can say that the list is divided to small parts). So i go to SL[0] explore the list, then go to SL[1] explore the list.....

typedef struct TSL {
   struct TSL *next;
   int a;
} LSL;

LSL* SL[n] = {0}; // Array of pointers ;)

// Loop 1
void Find_All_Arc_SL()
{
   int i;
   LSL *tmp;
   for(i=0;i<n;i++)
   {
      tmp = SL[i];
      while(tmp != 0)
      {
         //printf("I find arc! %d -> %d",i,tmp->a);
         tmp = tmp -> next;
      }
   }
}

Loop 2.

typedef struct TAL {
   struct TAL *next;
   int v;
   int a;
 } LAL;
LAL *AL = 0;

void Find_All_Arc_AL()
{

  LAL *tmp;
  tmp = AL;

  while(tmp != 0)
  {
    //printf("I find arc %d -> %d \n",tmp->v,tmp->a);
    tmp = tmp -> next;
  };

}

In this function i just explore list... just do it without any array etc.

My question is: Why Find_All_Arc_SL() is always faster (milliseconds) than Find_All_Arc_AL() ? These functions are working almost the same, but the first (faster one) one have to do additional work

YOU ASKED FOR FULL CODE. HERE IT IS: U can increase/decrease n

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define n 5500

//Define struct
typedef struct TSL {
   struct TSL *next;
   int a;
 } LSL;


typedef struct TAL {
   struct TAL *next;
   int v;
   int a;
 } LAL;


// Poiner and array of pointers
LAL *AL = 0;
LSL* SL[n] = {0};

// To Calculate time
__int64 freq, start, end, diff;


// Build graph
void Create_AL()
{

    LAL *tmp;
    int p,k;

 for(p=0;p<n;p++)
 for(k=0;k<n;k++)
 {
     // Add arc
    tmp = malloc (sizeof(LAL));
    tmp->v = p;
    tmp->a = k;

     if(AL == 0) { tmp->next = 0; AL = tmp; }
     else { tmp->next = AL; AL = tmp; }  
 }
}

// Find arc
void Find_All_Arc_AL()
{

    LAL *tmp;
    tmp = AL;

    while(tmp != 0)
    {
     //printf("I found arc %d -> %d \n",tmp->v,tmp->a);
     tmp = tmp -> next;
    };

}


// Build graph
void Create_SL()
{

    LSL *tmp;
    int p,k;

 for(p=0;p<n;p++)
 for(k=0;k<n;k++)
 { 
    // Add arc
    tmp = malloc(sizeof(LSL));
    tmp -> a = k;       

    if(SL[p] == 0) {  tmp -> next = 0; SL[p] = tmp; }
    else { tmp -> next = SL[p]; SL[p] = tmp; }
    }

}


void Find_All_Arc_SL()
{

 int i;
    LSL *tmp;


    for(i=0;i<n;i++)
    {
    tmp = SL[i];

        while(tmp != 0)
        {
        //printf("I find arc %d -> %d \n", i, tmp->a);
        tmp = tmp -> next;
        }

    }

}


/**
 ** CALCULATE TIME!
 **/

void start_timer()
{
 freq = 0; start = 0; end = 0; diff = 0;

    QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
 QueryPerformanceCounter((LARGE_INTEGER*)&start);

}

void end_timer()
{

    QueryPerformanceCounter((LARGE_INTEGER*)&end);
    diff = ((end - start) * 1000) / freq;

}

int main(int argc, char *argv[])
{

Create_SL();

start_timer();
 Find_All_Arc_SL();
end_timer();
printf("Find_All_Arc_SL SEARCHING ALL ARC TOOK %d \n",diff);



 Create_AL();

start_timer();
 Find_All_Arc_AL();
end_timer();
printf("Find_All_Arc_AL SEARCHING ALL ARC TOOK %d \n",diff);



  system("PAUSE");  
  return 0;
}

It depends on your data. You should post a full (working) example.

Also, how did you measure time? are you sure the comparison is significant?

It looks like a memory thing. Since accessing non-cached memory can take hundreds or thounsands of CPU cycles, the amount and the locality of memory access often is the most important factor influencing the performance of a program.

In your case, the SL structure is smaller than the AL structure. So Find_All_Arc_SL() has less memory to visit and is therefore faster.

But overall, the programm seems too bare bone to be a realistic test.

BTW: For improved performance, you should use more arrays and fewer linked lists because arrays have far better locality than linked lists.

You need to loop through the function to get a real feel for the speed. You also didn't warm-up the functions to get the values in the cache for the first method. The results I got were:

Find_All_Arc_SL SEARCHING ALL ARC TOOK 6657

Find_All_Arc_AL SEARCHING ALL ARC TOOK 6490

with this code:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define n 500

//Define struct
typedef struct TSL {
   struct TSL *next;
   int a;
 } LSL;


typedef struct TAL {
   struct TAL *next;
   int v;
   int a;
 } LAL;


// Poiner and array of pointers
LAL *AL = 0;
LSL* SL[n] = {0};

// To Calculate time
__int64 freq, start, end, diff;


// Build graph
void Create_AL()
{

    LAL *tmp;
    int p,k;

 for(p=0;p<n;p++)
 for(k=0;k<n;k++)
 {
     // Add arc
    tmp = malloc (sizeof(LAL));
    tmp->v = p;
    tmp->a = k;

     if(AL == 0) { tmp->next = 0; AL = tmp; }
     else { tmp->next = AL; AL = tmp; }  
 }
}

// Find arc
void Find_All_Arc_AL()
{

    LAL *tmp;
    tmp = AL;

    while(tmp != 0)
    {
     //printf("I found arc %d -> %d \n",tmp->v,tmp->a);
     tmp = tmp -> next;
    };

}


// Build graph
void Create_SL()
{

    LSL *tmp;
    int p,k;

 for(p=0;p<n;p++)
 for(k=0;k<n;k++)
 { 
    // Add arc
    tmp = malloc(sizeof(LSL));
    tmp -> a = k;       

    if(SL[p] == 0) {  tmp -> next = 0; SL[p] = tmp; }
    else { tmp -> next = SL[p]; SL[p] = tmp; }
    }

}


void Find_All_Arc_SL()
{

 int i;
    LSL *tmp;


    for(i=0;i<n;i++)
    {
    tmp = SL[i];

        while(tmp != 0)
        {
        //printf("I find arc %d -> %d \n", i, tmp->a);
        tmp = tmp -> next;
        }

    }

}


/**
 ** CALCULATE TIME!
 **/

void start_timer()
{
 freq = 0; start = 0; end = 0; diff = 0;

    QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
 QueryPerformanceCounter((LARGE_INTEGER*)&start);

}

void end_timer()
{

    QueryPerformanceCounter((LARGE_INTEGER*)&end);
    diff = ((end - start) * 1000) / freq;

}

int main(int argc, char *argv[])
{
    int i;
Create_SL();

 Find_All_Arc_SL();
start_timer();
for(i=0;i<2000;++i)
 Find_All_Arc_SL();
end_timer();
printf("Find_All_Arc_SL SEARCHING ALL ARC TOOK %d \n",diff);



 Create_AL();

 Find_All_Arc_AL();
start_timer();
for(i=0;i<2000;++i)
 Find_All_Arc_AL();
end_timer();
printf("Find_All_Arc_AL SEARCHING ALL ARC TOOK %d \n",diff);



  system("PAUSE");  
  return 0;
}

Edit: For what it's worth I had to lower your n , it was so big, malloc returned 0 on a 64-bit system with 4gb of ram.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM