Optimization: Accessing pointers in different memory areas

Assume there is the following case:

int** data = calloc(3, sizeof(int* ));

int* dynamicInt = calloc(1, sizeof(int));
int automaticInt = 0;
int stackint[1] = {0};

data[0] = dynamicInt;
data[1] = &automaticInt;
data[2] = stackint;

And then you would be navigating through values with, say, *data[i]

It seems to me that jumping from heap to stack will even be slower than from heap to another address in the heap.


What would be the fastest way of assigning those pointers? What would be really useful is if someone enumerate the possible variations (e.g: stack-stack: 6(fastest), heap-heap 5, auto-auto 4, heap-stack 3 etc..)

Upvotes: 2

Views: 43

Answers (2)

0___________
0___________

Reputation: 67546

The will be no difference even if the referenced address was swapped as you do not access it

Upvotes: 0

vicatcu
vicatcu

Reputation: 5837

I mean it's all in RAM right? Accessing the stack vs the heap is not signficantly different performance imho. Where you'll take a performance hit is in your spatial cache locality because you will potentially be hopping all over memory and taking cache misses. If you care about performance and must use a scheme like this, you might benefit somewhat by sorting the pointers by their value (which is an address). If your pointers are not changing much, though, you'll still probably benefit from temporal locality in the cache. Not sure this helps, but that's my two cents.

Upvotes: 1

Related Questions