从数组中读取比写入更快??? [英] Reading from array is faster than writing ???

查看:100
本文介绍了从数组中读取比写入更快???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

谁可以解释为什么来自array [] []的readind很快,但是

写(同一个地方)可能需要花费很多时间(我需要)很多循环)

如何解决它(声明为静态或强制一些编译器(VC 6.0)

选项)???在此先感谢

这是示例(但关键!!!)代码...


void main(int argc,char * argv [])

{

unsigned char r [360] [180],dat2,i,j,k;

int p;
< (b = 0; p <650; p ++){

for(i = 0; i <200; i ++){
(j = 0; j <200; j ++){

for(k = 0; k <100; k ++){


dat2 = r [j ][一世]; DAT2 = R [j]的[I]; dat2 = r [j] [i];


// r [j] [i] = dat2; [R [j]的[I] = DAT2; r [j] [i] = dat2;

//关键点:没有它几乎没有执行时间

//用它 - 在P3-1000上6秒? ?


}

}

}

}

}

解决方案

" Oleg Kornilov" <醇*********** @ mail.ru>写了

你好!
谁能解释为什么来自array [] []的readind很快,但写作(同一个地方)可能需要花费很多时间(我需要很多循环) )
如何解决它(声明为静态或强制一些编译器(VC 6.0)
选项)???提前致谢
以下是示例(但关键!!!)代码......

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;
//关键点:没有它几乎没有执行时间
//用它 - P3-1000上6秒???
}
}
}
}
}




我的第一个猜测是编译器(正确地)优化出你的读数

,因为结果值从未用于任何事情。


Claudio Puviani




" Oleg Kornilov" <醇*********** @ mail.ru>在消息中写道

news:9c ************************** @ posting.google.c om ...

你好!
谁能解释为什么来自array [] []的readind很快,但写作(同一个地方)可能需要花费很多时间(我需要很多$ b) $ b循环)如何解决它(声明为静态或强制一些编译器
(VC 6.0)选项)???提前致谢
以下是示例(但关键!!!)代码......

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;
//关键点:没有它几乎没有执行时间
//用它 - P3-1000上6秒???
}
}
}
}
}




你不能在数组边界外访问吗? br />


Oleg Kornilov写道:

你好!
谁能解释为什么来自array [] []的readind很快,但是写(同一个地方)可能需要花费很多时间(我需要很多循环)
如何解决它(声明为静态或强制某些编译器(VC 6.0)
选项)?? ?在此先感谢
这是示例(但关键!!!)代码...



一个可能的答案可能是缓存。另一个答案可能是

优化:如果你不使用你在编译器中读到的值可能完全省略

循环。

一些不错的性能提示:

*使用unsigned int / size_t - 循环指示的变量。

*使用Blitz ++数组或1D-valarray存储数据:

valarray< unsigned char> r(360 * 180);

r [180 * row + column] = value;

*使用超标量体系结构/ SIMD调用

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);


虽然它为Athlons提供了一些不错的提示其他现代机器仍然可用:
http://www.amd.com/us-en/assets/cont...te_papers_and_

tech_docs / 22007.pdf


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 ???

}
}
}
}
}

解决方案

"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



"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?


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


这篇关于从数组中读取比写入更快???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆