473,385 Members | 1,325 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,385 software developers and data experts.

handling with arrays, what will be faster???

Dear all,

In C, I am wondering what codes will run faster:
assuming, I have two arrays: dounle a[1000], double [1000]

code 1:
for (i=1;i<1000;i++)
a[i]=100*a[i]+300*b[i];

or

code 2:

double tem3;
for(i=1;i<1000;i++){
tem1=a[i];
tem2=b[i];
tem3=100*tem1+300*tem2;
a[i]=tem3;
}

Thanks

May 4 '07 #1
7 1554
A.E lover wrote:
Dear all,

In C, I am wondering what codes will run faster:

Profile it and see!

--
Ian Collins.
May 4 '07 #2
A.E lover wrote:
Dear all,

In C, I am wondering what codes will run faster:
Is this a trick question ?
May 4 '07 #3
I don't understand what you mean.

when I am programming, I am not sure if we should put directly the
array's elements in to a formula or use some temporary variables
instead. I heard somewhere in C tips that this will be faster since it
doesn't need to frequently access to the arrays.
On May 4, 6:00 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
A.E lover wrote:
Dear all,
In C, I am wondering what codes will run faster:

Is this a trick question ?

May 4 '07 #4
A.E lover wrote:
>
Please don't top-post
>
On May 4, 6:00 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>A.E lover wrote:
>>Dear all,
In C, I am wondering what codes will run faster:
Is this a trick question ?

I don't understand what you mean.

when I am programming, I am not sure if we should put directly the
array's elements in to a formula or use some temporary variables
instead. I heard somewhere in C tips that this will be faster since it
doesn't need to frequently access to the arrays.
You appear to have a case of premature optimisation. Let the compiler
look after those details and if your subsequent measurements show it
can't, optimise then.

--
Ian Collins.
May 4 '07 #5
A.E lover wrote:
I don't understand what you mean.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
May 4 '07 #6
A.E lover wrote:
I don't understand what you mean.

when I am programming, I am not sure if we should put directly the
array's elements in to a formula or use some temporary variables
instead. I heard somewhere in C tips that this will be faster since it
doesn't need to frequently access to the arrays.
On May 4, 6:00 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>A.E lover wrote:
>>Dear all,
In C, I am wondering what codes will run faster:
Is this a trick question ?

OK. Now I understand your question better (I think).

Well, yes and no. As pointed out by another poster, the only way to
know is to profile it on the platform(s) of choice and see.

The yes - if you can minimize access to an array element, then the
compiler has less to optimize.

And no - optimizers are getting very very good. Your code above may be
vectorizable and the temporaries you create would make it harder for the
vectorizer to recognize.

The code you posted would more than likely create exactly the same
assembler code out of a modern compiler.

So lets see what the compiler generates when you send it this:

typedef struct A
{
double a[1000];
double b[1000];
} A;

void foo( A * s )
{
int i;

for (i=1;i<1000;i++)
{
s->a[i]=100*s->a[i]+300*s->b[i];
}
}

void foox( A * s )
{
int i;

double atemp;
double btemp;
double rtemp;

for (i=1;i<1000;i++)
{
atemp = s->a[i];
btemp = s->b[i];
rtemp=100*atemp+300*btemp;
s->a[i] = rtemp;
}
}

int main()
{
A s[1];

foo( s );
}

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvvvv
gcc -O3 -S xxx_optimizer.c
gcc (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1) Target: x86_64-redhat-linux
.file "xxx_optimizer.c"
.section .rodata.cst8,"aM",@progbits,8
.align 8
..LC0:
.long 0
.long 1079574528
.align 8
..LC1:
.long 0
.long 1081262080
.text
.p2align 4,,15
..globl foo
.type foo, @function
foo:
..LFB2:
movsd .LC0(%rip), %xmm3
xorl %eax, %eax
movsd .LC1(%rip), %xmm2
.p2align 4,,7
..L2:
movsd 8(%rdi,%rax,8), %xmm0
movsd 8008(%rdi,%rax,8), %xmm1
mulsd %xmm3, %xmm0
mulsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 8(%rdi,%rax,8)
addq $1, %rax
cmpq $999, %rax
jne .L2
rep ; ret
..LFE2:
.size foo, .-foo
.section .rodata.cst8
.align 8
..LC2:
.long 0
.long 1079574528
.align 8
..LC3:
.long 0
.long 1081262080
.text
.p2align 4,,15
..globl foox
.type foox, @function
foox:
..LFB3:
movsd .LC2(%rip), %xmm3
xorl %eax, %eax
movsd .LC3(%rip), %xmm2
.p2align 4,,7
..L9:
movsd 8(%rdi,%rax,8), %xmm0
movsd 8008(%rdi,%rax,8), %xmm1
mulsd %xmm3, %xmm0
mulsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 8(%rdi,%rax,8)
addq $1, %rax
cmpq $999, %rax
jne .L9
rep ; ret
.... snipped ....
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^

OK - so it appears to have used the vector instructions but it looks
like it's not actually vectorizing the code. As you can see however,
the code is identical for both functions.
May 5 '07 #7
On May 5, 12:33 am, "A.E lover" <aelove...@gmail.comwrote:
I don't understand what you mean.
when I am programming, I am not sure if we should put directly the
array's elements in to a formula or use some temporary variables
instead. I heard somewhere in C tips that this will be faster since it
doesn't need to frequently access to the arrays.
Which is just bullshit.

In the actual example you posted, I can't imagine a compiler
generating different code for the two cases. More generally, of
course, if you write your code cleanly, it will be simple to
optimize it later if it turns out to be necessary. If you
don't, it will be more difficult to optimize once you know where
the problems are.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 5 '07 #8

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

Similar topics

1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
9
by: Till Crueger | last post by:
Hi, I have to implement some simple sorting algorithm. I am NOT asking for you to do my homework, but my question is rather on how to store the integers. I recall reading once in here that there...
1
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small...
38
by: Peteroid | last post by:
I looked at the addresses in an 'array<>' during debug and noticed that the addresses were contiguous. Is this guaranteed, or just something it does if it can? PS = VS C++.NET 2005 Express...
5
by: PJ6 | last post by:
I've chosen to store the contents of a grid in a two dimensional object array. As the size of the grid changes, so must the array. I was surprised to find out that the niceties I'm used to with one...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
0
by: Folkert van Heusden | last post by:
Hi, I have 2 arrays: $b $s on which I would like to do - set each field to ' ' ($b) and to 0 ($s) - make a copy of both whole arrays into other arrays Now normally I would for($y=0; $y<8;...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
gits
by: gits | last post by:
This little article will show you how to optimize runtime performance when you need to compare two arrays (a quite common task). Have a close look at the entire article, and you will see the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?

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.