Reading from array is faster than writing ???

O

Oleg Kornilov

Hello !
Who can explain why readind from array[][] is fast, but
writing (same place) might take a lot of time (I need many loops)
How to solve it (declare as static or force some compiler (VC 6.0)
options) ??? Thanks in advance
Here is example (but critical !!!) code...

void main(int argc, char* argv[])
{
unsigned char r[360][180], dat2, i,j,k;
int p;

for (p=0; p<650; p++) {
for (i=0; i<200; i++) {
for (j=0; j<200; j++) {
for (k=0; k<100; k++) {

dat2=r[j]; dat2=r[j]; dat2=r[j];

// r[j]=dat2; r[j]=dat2; r[j]=dat2;
// critical point: without it almost no execution time
// with it - 6 seconds on P3-1000 ???

}
}
}
}
}
 
C

Claudio Puviani

Oleg Kornilov said:
Hello !
Who can explain why readind from array[][] is fast, but
writing (same place) might take a lot of time (I need many loops)
How to solve it (declare as static or force some compiler (VC 6.0)
options) ??? Thanks in advance
Here is example (but critical !!!) code...

void main(int argc, char* argv[])
{
unsigned char r[360][180], dat2, i,j,k;
int p;

for (p=0; p<650; p++) {
for (i=0; i<200; i++) {
for (j=0; j<200; j++) {
for (k=0; k<100; k++) {

dat2=r[j]; dat2=r[j]; dat2=r[j];

// r[j]=dat2; r[j]=dat2; r[j]=dat2;
// critical point: without it almost no execution time
// with it - 6 seconds on P3-1000 ???

}
}
}
}
}


My first guess is that the compiler is (correctly) optimizing out your reads
since the resulting value is never used for anything.

Claudio Puviani
 
J

John Ericson

Oleg Kornilov said:
Hello !
Who can explain why readind from array[][] is fast, but
writing (same place) might take a lot of time (I need many loops)
How to solve it (declare as static or force some compiler (VC 6.0)
options) ??? Thanks in advance
Here is example (but critical !!!) code...

void main(int argc, char* argv[])
{
unsigned char r[360][180], dat2, i,j,k;
int p;

for (p=0; p<650; p++) {
for (i=0; i<200; i++) {
for (j=0; j<200; j++) {
for (k=0; k<100; k++) {

dat2=r[j]; dat2=r[j]; dat2=r[j];

// r[j]=dat2; r[j]=dat2; r[j]=dat2;
// critical point: without it almost no execution time
// with it - 6 seconds on P3-1000 ???

}
}
}
}
}


Aren't you accessing outside your array bounds?
 
R

Ruediger Knoerig

Oleg said:
Hello !
Who can explain why readind from array[][] is fast, but
writing (same place) might take a lot of time (I need many loops)
How to solve it (declare as static or force some compiler (VC 6.0)
options) ??? Thanks in advance
Here is example (but critical !!!) code...
One possible answer could be the cache. Another answer could be the
optimization: if you don't use the values you read in the compiler may omit
the loop completely.
Some nice performance tips:
* use unsigned int / size_t - variables for loop indicies.
* use Blitz++ arrays or 1D-valarrays for storing the data:
valarray<unsigned char> r(360*180);
r[180*row+column]=value;
* make usage of the superscalar architecture / SIMD calls
for(size_t i=0;i < 100;i++) value += data;
to
sum1=sum2=sum3=sum4=0.0;
for(size_t i=0;i < 100; i+=4)
{
sum1 += data;
sum2 += data[i+1];
sum3 += data[i+2];
sum4 += data[i+3];
}
sum1 += (sum2 + sum3 + sum4);

Although its for Athlons some nice tips are still usuable on
other modern machines:
http://www.amd.com/us-en/assets/content_type/white_papers_and_
tech_docs/22007.pdf
 
V

Victor Bazarov

John Ericson said:
Oleg Kornilov said:
Hello !
Who can explain why readind from array[][] is fast, but
writing (same place) might take a lot of time (I need many loops)
How to solve it (declare as static or force some compiler (VC 6.0)
options) ??? Thanks in advance
Here is example (but critical !!!) code...

void main(int argc, char* argv[])
{
unsigned char r[360][180], dat2, i,j,k;
int p;

for (p=0; p<650; p++) {
for (i=0; i<200; i++) {
for (j=0; j<200; j++) {
for (k=0; k<100; k++) {

dat2=r[j]; dat2=r[j]; dat2=r[j];

// r[j]=dat2; r[j]=dat2; r[j]=dat2;
// critical point: without it almost no execution time
// with it - 6 seconds on P3-1000 ???

}
}
}
}
}


Aren't you accessing outside your array bounds?


Nah, 'j' never goes above 199, which is well within 0..359 space,
so when 'i' goes above 179 (for only 20), he's not stepping on any
other memory, only the next 20 elements of the 'j+1' array or the
'r' superarray.

V
 
J

John Ericson

Victor Bazarov said:
John Ericson said:
Oleg Kornilov said:
Hello !
Who can explain why readind from array[][] is fast, but
writing (same place) might take a lot of time (I need
many
loops)
How to solve it (declare as static or force some
compiler
(VC 6.0)
options) ??? Thanks in advance
Here is example (but critical !!!) code...

void main(int argc, char* argv[])
{
unsigned char r[360][180], dat2, i,j,k;
int p;

for (p=0; p<650; p++) {
for (i=0; i<200; i++) {
for (j=0; j<200; j++) {
for (k=0; k<100; k++) {

dat2=r[j]; dat2=r[j]; dat2=r[j];

// r[j]=dat2; r[j]=dat2; r[j]=dat2;
// critical point: without it almost no execution time
// with it - 6 seconds on P3-1000 ???

}
}
}
}
}


Aren't you accessing outside your array bounds?


Nah, 'j' never goes above 199, which is well within 0..359 space,
so when 'i' goes above 179 (for only 20), he's not stepping on any
other memory, only the next 20 elements of the 'j+1' array or the
'r' superarray.

V


:) You've got a point <ROTFL> - Victor, I sure enjoy your
posts!

Best regards, JE
 
A

alexhong2001

Ruediger Knoerig said:
Oleg said:
Hello !
Who can explain why readind from array[][] is fast, but
writing (same place) might take a lot of time (I need many loops)
How to solve it (declare as static or force some compiler (VC 6.0)
options) ??? Thanks in advance
Here is example (but critical !!!) code...
One possible answer could be the cache. Another answer could be the
optimization: if you don't use the values you read in the compiler may omit
the loop completely.
Some nice performance tips:
* use unsigned int / size_t - variables for loop indicies.

Would you elabrate on this for us please?
* use Blitz++ arrays or 1D-valarrays for storing the data:

What is Blitz++ arrays?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top