473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading from array is faster than writing ???

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][i]; dat2=r[j][i]; dat2=r[j][i];

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

}
}
}
}
}
Jul 22 '05 #1
6 2481
"Oleg Kornilov" <ol***********@mail.ru> wrote
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][i]; dat2=r[j][i]; dat2=r[j][i];

// r[j][i]=dat2; r[j][i]=dat2; r[j][i]=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
Jul 22 '05 #2

"Oleg Kornilov" <ol***********@mail.ru> wrote in message
news:9c**************************@posting.google.c om...
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][i]; dat2=r[j][i]; dat2=r[j][i];

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

}
}
}
}
}


Aren't you accessing outside your array bounds?
Jul 22 '05 #3
Oleg Kornilov wrote:
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[i];
to
sum1=sum2=sum3=sum4=0.0;
for(size_t i=0;i < 100; i+=4)
{
sum1 += data[i];
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/cont...te_papers_and_
tech_docs/22007.pdf

Jul 22 '05 #4
"John Ericson" <je************@pacbell.net> wrote...

"Oleg Kornilov" <ol***********@mail.ru> wrote in message
news:9c**************************@posting.google.c om...
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][i]; dat2=r[j][i]; dat2=r[j][i];

// r[j][i]=dat2; r[j][i]=dat2; r[j][i]=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
Jul 22 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:r26Xb.27653$uV3.49996@attbi_s51...
"John Ericson" <je************@pacbell.net> wrote...

"Oleg Kornilov" <ol***********@mail.ru> wrote in message
news:9c**************************@posting.google.c om...
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][i]; dat2=r[j][i]; dat2=r[j][i];

// r[j][i]=dat2; r[j][i]=dat2; r[j][i]=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
Jul 22 '05 #6
Ruediger Knoerig <ru******@knoerig.de> wrote in message
news:c0**********@mamenchi.zrz.TU-Berlin.DE...
Oleg Kornilov wrote:
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?
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
15
by: Oleg Kornilov | last post by:
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)...
4
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
1
by: dsmith | last post by:
I need to write a data array out to disk, but unfortunately am running into performance issues. The function is essentially: private ushort theInt16Array = new ushort; public void...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
by: John | last post by:
Hi I am trying to save settings of controls on my form to a file so I can read them back later and recreate the controls on the form. I have figured out how to go through all controls and get...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.