C++ structure: more members, MUCH slower member access time?
<p>I have a linked list of structures. Lets say I insert x million nodes into the linked list,
then I iterate trough all nodes to find a given value.</p>

<p>The strange thing is (for me at least), if I have a structure like this:</p>

<pre><code>struct node
    {
    int a;
    node *nxt;
    };
</code></pre>

<p>Then I can iterate trough the list and check the value of a ten times faster compared to when I have another member in the struct, like this:</p>

<pre><code>struct node_complex
   {
   int a;
   string b;
   node_complex *nxt;
   };
</code></pre>

<p>I also tried it with C style strings (char array), the result was the same: <strong><em>just because I had another member (string), the whole iteration (+ value check) was 10 times slower</em></strong>, even if I did not even touched that member ever! Now, I do not know how the internals of structures work, but it looks like a high price to pay...</p>

<p>What is the catch?</p>

<p><strong>Edit</strong>:
I am a beginner and this is the first time I use pointers, so chances are, the mistake is on my part. I will post the code ASAP (not being at home now).</p>

<p><strong>Update</strong>:
I checked the values again, and I know see a much smaller difference: 2x instead of 10x.
It is much more reasonable for sure.</p>

<p>While it is certainly possible it was the case yesterday too and I was just so freaking tired last night I could not divide two numbers, I have just made more tests and the results are mind blowing.</p>

<p>The times for a the same number of nodes is:</p>

<ol>
<li>One int and a pointer the time to iterate trough is 0.101</li>
<li>One int and a string: 0.196</li>
<li>One int and 2 strings: 0.274</li>
<li>One int and 3 strings: 0.147 (!!!)</li>
<li>For two ints it is: 0.107</li>
</ol>

<p>Look what happens when there is more than two strings in the structure! It gets faster! Did somebody drop LSD into my coffee? No! I do not drink coffee.</p>

<p>It is way too fckd up for my brain at the mo' so I think I will just figure it out on my own instead of draining public resources here at SO.</p>

<p>(Ad: I do not think my profiling class is buggy, and anyway I can see the time difference with my own eyes).</p>

<p>Anyhow, thanks for the help.
Cheers.</p>
