I have a linked list of structures. Suppose I insert x million nodes into a linked list, then I repeat all the nodes to find the given value.
The strange thing (for me, at least) if I have a structure like this:
struct node { int a; node *nxt; };
Then I can iterate through the list and check the value ten times faster compared to when I have another member in the structure, for example:
struct node_complex { int a; string b; node_complex *nxt; };
I also tried it using C style strings (char array), the result was the same: just because I had a different member (string), the whole iteration (+ checking the value) was 10 times slower , even if I didn't even touch this member! Now I don’t know how internal structures work, but it looks like a high price to pay ...
What is the trick?
Edit : I'm a newbie, and this is the first time I use pointers, so it's most likely an error on my part. I will send the code as soon as possible (not being at home now).
Update : I checked the values again, and I know that the difference is much smaller: 2x instead of 10x. This is much more reasonable.
Although this, of course, was possible, it was yesterday, and I was so exhausted that I couldn’t separate the two numbers yesterday, I just did more tests and the results are bloated.
Time for the same number of nodes:
- One int and time pointer for iteration trough 0.101
- One int and line: 0.196
- One int and 2 lines: 0.274
- One int and 3 lines: 0.147 (!!!)
- For two ints: 0.107
See what happens when there are more than two lines in a structure! It is getting faster! Has someone thrown LSD in my coffee? No! I do not drink coffee.
This is too much for my brain in the moment, so I think I’ll just think about it on my own, instead of depleting the public resources here in SO.
(Ad: I don’t think my profiling class is buggy, and in any case, I see the time difference with my own eyes).
Anyway, thanks for the help. Greetings.