Note that there are some explanatory texts on larger screens.

plurals
  1. POShould be slower, but is faster. Why?
    primarykey
    data
    text
    <p>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..... </p> <pre><code>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&lt;n;i++) { tmp = SL[i]; while(tmp != 0) { //printf("I find arc! %d -&gt; %d",i,tmp-&gt;a); tmp = tmp -&gt; next; } } } </code></pre> <p>Loop 2. </p> <pre><code>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 -&gt; %d \n",tmp-&gt;v,tmp-&gt;a); tmp = tmp -&gt; next; }; } </code></pre> <p>In this function i just explore list... just do it without any array etc.</p> <p><strong>My question is:</strong> Why <code>Find_All_Arc_SL()</code> is always faster (milliseconds) than <code>Find_All_Arc_AL()</code>? These functions are working almost the same, but the first (faster one) one have to do additional work</p> <p><strong>YOU ASKED FOR FULL CODE. HERE IT IS:</strong> U can increase/decrease <strong>n</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;windows.h&gt; #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&lt;n;p++) for(k=0;k&lt;n;k++) { // Add arc tmp = malloc (sizeof(LAL)); tmp-&gt;v = p; tmp-&gt;a = k; if(AL == 0) { tmp-&gt;next = 0; AL = tmp; } else { tmp-&gt;next = AL; AL = tmp; } } } // Find arc void Find_All_Arc_AL() { LAL *tmp; tmp = AL; while(tmp != 0) { //printf("I found arc %d -&gt; %d \n",tmp-&gt;v,tmp-&gt;a); tmp = tmp -&gt; next; }; } // Build graph void Create_SL() { LSL *tmp; int p,k; for(p=0;p&lt;n;p++) for(k=0;k&lt;n;k++) { // Add arc tmp = malloc(sizeof(LSL)); tmp -&gt; a = k; if(SL[p] == 0) { tmp -&gt; next = 0; SL[p] = tmp; } else { tmp -&gt; next = SL[p]; SL[p] = tmp; } } } void Find_All_Arc_SL() { int i; LSL *tmp; for(i=0;i&lt;n;i++) { tmp = SL[i]; while(tmp != 0) { //printf("I find arc %d -&gt; %d \n", i, tmp-&gt;a); tmp = tmp -&gt; next; } } } /** ** CALCULATE TIME! **/ void start_timer() { freq = 0; start = 0; end = 0; diff = 0; QueryPerformanceFrequency((LARGE_INTEGER*)&amp;freq); QueryPerformanceCounter((LARGE_INTEGER*)&amp;start); } void end_timer() { QueryPerformanceCounter((LARGE_INTEGER*)&amp;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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload