473,608 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2503
"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.goo gle.com...
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<unsign ed 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.goo gle.com...
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.********@com Acast.net> wrote in message
news:r26Xb.2765 3$uV3.49996@att bi_s51...
"John Ericson" <je************ @pacbell.net> wrote...

"Oleg Kornilov" <ol***********@ mail.ru> wrote in message
news:9c******** *************** ***@posting.goo gle.com...
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******@knoer ig.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
1775
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') >>> f.write('dan\n') >>> f.write('pat\n') >>> f.close()
9
2559
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 java: 325 gcc: 348 Python with Psyco: 1317 Pure Python using sum: 2312
2
3061
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 array). I cannot change anything in the program but can add some features by which I can convert this array of data into a file. The easiest thing would be to write the data into a file (in hard disk) and use it. But I will be working on thousands...
15
539
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) options) ??? Thanks in advance Here is example (but critical !!!) code... void main(int argc, char* argv) { unsigned char r, dat2, i,j,k;
4
5966
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 the data in. However, it takes about 20 seconds to complete and I wonder if there is a faster way. For example, I found that I could use 'fread' to read the data into a string that looks like this:
3
9500
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
1
4076
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 Save(BinaryWriter bw) { /// Far too slow: // for (int row = 0; row < Rows; row++) // {
21
13069
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 server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
6
5255
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
1598
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 their properties (see code below). What I am not sure is; how and what type of file format I need to save the info to and how to read it back. Would appreciate any help on that. A code snippet would be very much appreciated. Many thanks
0
8063
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8498
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8152
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6817
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.