Vous êtes sur la page 1sur 3

Using Field Symbols in Loop Statements - Performance Boost?

Subscribe
Rich Heilman Print
Business Card Permalink
Company: SAP Labs, LLC Share
Posted on Mar. 07, 2006 01:08 PM in ABAP, Beginner

The Question

The other day, someone asked a question in the ABAP forum, "When using the ASSIGNING
extension of the LOOP statement, is performance improved". To tell the true, I didn't really
know for sure, but I had seen somewhere that using field symbols is faster. Until recently, I never
had the time to try it out. Here are the results of the study.

The Program

I wrote a simple ABAP program to do some measurements. The program creates data in an
internal able, then does two loops. The first loop will loop the internal table in the "normal"
fashion, the second, will use the ASSIGNING extension. Both loops will write out the line of the
internal table. At the end, the runtimes for both loops will be written.
report zloop_performance_test.

types: begin of ttab,


fld1(10) type c,
fld2(10) type c,
fld3(10) type c,
fld4(10) type c,
fld5(10) type c,
end of ttab.

data: itab type table of ttab,


xtab like line of itab.

data: rt_str type i,


rt_end type i,
run_time1 type p decimals 2,
run_time2 type p decimals 2.

field-symbols: <fs_tab> type table.


field-symbols: <fs> type ttab.

start-of-selection.

* Create data in an internal table


do 100000 times.
xtab-fld1 = sy-index.
xtab-fld2 = sy-index * 10.
xtab-fld3 = sy-index * 100.
append xtab to itab.
enddo.

* Check performance for "normal" loop.


get run time field rt_str.
loop at itab into xtab.
w rite:/ xtab-fld1, xtab-fld2, xtab-fld3.
endloop.
get run time field rt_end.
run_time1 = ( rt_end - rt_str ) / 1000000 .

skip 1.

* Check perfomance for "assigning" loop


get run time field rt_str.
loop at itab assigning <fs>.
w rite:/ <fs>-fld1, <fs>-fld2, <fs>-fld3.
endloop.
get run time field rt_end.
run_time2 = ( rt_end - rt_str ) / 1000000 .

new -page.

w rite:/ 'Loop at itab into.......', run_time1, 'seconds'.


w rite:/ 'Loop at itab assigning..', run_time2, 'seconds'.
The Result

I was surprised to see about a 1 second difference between the two loops. Using field symbols in
the LOOP statement definitely did improve the performance. If processing a huge amount of
data, using the ASSIGNING extention could really help out the overall runtime.

Run this test program in your system and see if you get the same results. Feel free to put your
results in the comment section.