Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28591 Discussions

Whole array assignment vs do loop assignment

Les_Neilson
Valued Contributor II
279 Views
I think I have been watching to much TV,in particular the TV detective Mr Monk (Obsessive compulsive, feels the need to tidy upany mess he sees!)

I get like that when I look at code :-)

Some code I am looking at has *lots* of
call fill(n, value, array)
which does a loop 1 to n and assigns the scalar "value" to the array element-by-element

My "Mr Monk" instinct was to replace these calls with simple
array = value
statements
But I needed to know if it would make any actual difference to the performance.

So I did a simple test using cpu_time one around a loop which called "fill" lots of times and another around a loop which used array assignment.

I found that array assignment in debug mode isalmost 2/3 the time of the do loop (22.71 seconds vs 30.17 seconds)
and in release mode is less then half (3.38 second vs 8.00 seconds)

Such an improvement in performance may help me to persuadeothers that my "tidying up" *does* make a difference and not just in readability :-)


Les
0 Kudos
3 Replies
GVautier
New Contributor II
279 Views
Hello

Do you compare with :
array(:)=value

This syntax looks , for me,less confusive?

0 Kudos
Steven_L_Intel1
Employee
279 Views
Please, please, PLEASE! Do NOT use "array(:)" - it has subtly different semantics and can slow down the code. See Doctor, It Hurts When I Do This!
0 Kudos
Les_Neilson
Valued Contributor II
279 Views
Quoting gvautier
Hello

Do you compare with :
array(:)=value

This syntax looks , for me,less confusive?


What you have there is an array section.
Even thoughit applies to the whole array it is still a section. You should only use sections when you need to refer to an actualsection. (Maybe I should do a test involving sections and equivalent do loops.)

If there is a possibility of confusion I usually add a comment before the assignment informing the personreading the code(which could be me in the future) that it is an array.

Les

0 Kudos
Reply