Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Loop - Endloop

lijisusan_mathews
Active Contributor
0 Likes
1,013

HI,

will there be vast difference in execution time in the following two codes.

1) loop at itab into work area.

.

.some calculation with workarea

modify itab from workarea

endloop.

loop at itab into workarea.

read table from it_kna1 into wa_kna1.

some assignmtn into workarea.

modify itab from workarea.

AND

2)

loop at itab into work area.

. read table from it_kna1 into wa_kna1.

some assignmtn into workarea.

.some calculation with workarea

modify itab from workarea

endloop.

ie. i need to modify the values in my itab following differnrnt conditions like either calculations or updationg the address fieldor so.. If I can place all the different modifications on itab in different loops, the code will be more clear.. But will that affect my performance?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
936

2) loop at itab into work area.

. read table from it_kna1 into wa_kna1.

some assignmtn into workarea.

.some calculation with workarea

modify itab from workarea

endloop.

this code will be good in performance wise.....

try to avoid multiple loop..

8 REPLIES 8
Read only

Former Member
0 Likes
937

2) loop at itab into work area.

. read table from it_kna1 into wa_kna1.

some assignmtn into workarea.

.some calculation with workarea

modify itab from workarea

endloop.

this code will be good in performance wise.....

try to avoid multiple loop..

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
936

Guess ur having 100 records in itab.

First u have looped 100 times and done some anomalies.

Again u loop 100 times and do some changes.

Here the Loop count is 200 times for two operations..

instead do it in a single loop.

Got it...

Performance is better than readability

Read only

0 Likes
936

>

> Performance is better than readability

Not always. In fact, often it isn't. With most programs that you're likely to encounter, performance won't really be an issue. While we should endeavour to write programs that perform well, readability is very important as well. The reason is: a readable program is easier to fix/maintain/enhance, and therefore cheaper - most of the TCO of a development is not in the original development, but in the subsequent support. Emphasising performance at the expense of readability should only be when performance is critical. I.e. there is a business requirement for the program to run faster. So, if a program runs every night and takes 10 minutes, and will never take more than that, who cares if you can get the run time down to 9 minutes...

Anyway, in the supplied example, 2) is the correct approach. To keep readability, simply put the actions into some forms. Also, in this occasion, depending on the quantity of data, use LOOP AT ASSIGNING, and remove the MODIFY.

loop at itab assigning <fs>.
  perform do_stuff using it_kna1 changing <fs>.
  perform do_other_stuff changing <fs>.
endloop.

matt

Read only

Former Member
0 Likes
936

Hello

The second loop options that you ve given is best inregard from the point of performance basis.

2)

loop at itab into work area.

. read table from it_kna1 into wa_kna1.

some assignmtn into workarea.

.some calculation with workarea

modify itab from workarea

endloop.

It will loop the internal table as many times as the record present in the internal table.

Read only

Former Member
0 Likes
936

While the second option is slightly better, a much greater improvement will be gained by changing:

read table from it_kna1 into wa_kna1.

to:

read table from it_kna1 into wa_kna1 binary search.

(You will have to sort before reading).

Rob

Read only

matt
Active Contributor
0 Likes
936

Or even better, using a HASHED table with a unique key. ( If there isn't a unique key, use a SORTED table ).

matt

Read only

Former Member
0 Likes
936

Hi,

If your Logic for the Modification's is really complex and you feel that the report will be heavily used and hence modified..better go for the first option......Any program should be made with an approach that its going to be worked upon by n number of programmers in future and if your Logic is difficult to understand by your sucessive programmers that the cost of modifying & Maintaining the report will always be more than the required and hence an indirect cost to your Client.

But the above can never be used as a thumb rule...it all actually depends on the situtaion...like the number of records your internal table will have at the runtime, because with fewer number of records expected at the run time ..you can easily go with the second option.

So try to make a perfect and situtaion based balance between the performance and readibility/Simplicity of your code......taking into consideration that both are equally important......

Read only

lijisusan_mathews
Active Contributor
0 Likes
936

Thanx all.. I had to do some complex operations and a lot of read statements from several tables to insert the values into a single table in the first loop.. actually i have to set the running number based on a few criteria for each of the record. So i was thinking of modifying that value separately (along with provingthe address details)in another loop depending on the criteria..This can be achieved in a single loop too, but it will reduce teh clarity of the program and it is a critical report.. But I guess using the Perform and read using binary search can be used to solve my clarity and performance issue..Thanx a lot all.

Edited by: Suzie on Nov 3, 2008 5:10 PM