(I was confused about the multiplication time.Please see 774RR's response.)
Suppose you have a large array of about 1000 x 1000 (such as an image).i
To get the first address on the line,
double lightness[1000][1000];
// ...
long i = 200; // where i = 200
double*pLightness=lightness[i];
If you repeat this for i
from 0
to 999
, the more i
, the more time it takes to get an address.(Because of the []
specification)
for(i=0;i<1000;i++){
* pLightness=lightness[i]; // The first address in line i
// ... (Working with Addresses
}
Is there a faster way to get the first address of the i
line of the matrix from i=0
to 999
?
"If possible, I wish there was a faster and more elegant way than ""pLightness+=1000
""…
The motivation for the question is that if you get an address in the i
row j
column, you can get an address in the right-next, i
row j+i
column by increasing the pointer containing the address.
//(i,j)=(300,0) to (300,999) using the address pLightness of lightness[i][j]
i = 300;
pLightness=lightness[i]; // The first address on line 300
for (long j=0; j<1000;j++, pLightness++) {
// ... (Working with Addresses
}
Well, it's a basic question, so you might be asking me to read a reference book or use my brain a little more, but please forgive me.
c array pointer
If you repeat this for i from 0 to 999 then as i increases, the time it takes to get an address will increase.
The calculation time of lightness[i]
is O(1) for all processing systems that Euler can think of, i.e., the processing time is constant regardless of the value of i
.I can't imagine a hardware software implementation that doesn't become constant.
&lightness[i][j]
already exists
&lightness[i][j+1]
costs and
&lightness[i+1][j]
costs
Yes, there is a difference, but it is an assembly instruction and a few instructions and a range of errors.The cache hit miss penalty is overwhelmingly higher when starting access after calculating the pointer value.
© 2024 OneMinuteCode. All rights reserved.