好的,让我解释一下这个问题。在数组SL []中,我得到了列表的指针(我可以说列表被分成了小部分)。所以我去SL [0]探索列表,然后去SL [1]探索列表......
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;
}
}
}
循环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;
};
}
在这个函数中,我只是探索列表......只需要没有任何数组等。
我的问题是:为什么Find_All_Arc_SL()
总是比Find_All_Arc_AL()
更快(毫秒)?这些功能几乎相同,但第一个(更快)的功能必须做额外的工作
你要求完整的代码。这是: U可以增加/减少 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;
}
答案 0 :(得分:1)
这取决于您的数据。你应该发布一个完整的(工作)例子。
另外,你是如何衡量时间的?你确定比较重要吗?
答案 1 :(得分:0)
它看起来像一个记忆的东西。由于访问非缓存内存可能需要数百或数百个CPU周期,因此内存访问的数量和位置通常是影响程序性能的最重要因素。
在您的情况下,SL结构小于AL结构。因此Find_All_Arc_SL()
访问的内存较少,因此更快。
但总的来说,该程序似乎太过于无法进行真实的测试。
BTW:为了提高性能,你应该使用更多的数组和更少的链表,因为数组的位置比链表好得多。
答案 2 :(得分:0)
您需要遍历该功能才能真正感受到速度。您也没有预热函数来获取第一个方法的缓存中的值。我得到的结果是:
Find_All_Arc_SL搜索所有ARC到6657
Find_All_Arc_AL搜索所有ARC TOOK 6490
使用此代码:
#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;
}
编辑:为什么我不得不降低你的n
,它是如此之大,malloc
在64位系统上返回0,内存为4GB。