ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Character String and Byte String Processing → Expressions and Functions for String Processing → string_exp - String Expressions → string_exp - Performance Note
Chaining Strings
This example demonstrates how string chainings can be optimized.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA(n) = 10000.
cl_demo_input=>request( CHANGING field = n ).
IF n <= 0 OR n >= 100000.
EXIT.
ENDIF.
GET RUN TIME FIELD DATA(t1).
DATA(result1) =
REDUCE string( INIT s = ``
FOR i = 1 UNTIL i > n
NEXT s = s && CONV string( i ) ).
GET RUN TIME FIELD DATA(t2).
DATA(t21) = t2 - t1.
GET RUN TIME FIELD DATA(t3).
DATA(result2) =
REDUCE string( INIT s = ``
FOR i = 1 UNTIL i > n
LET num = CONV string( i ) IN
NEXT s = s && num ).
GET RUN TIME FIELD DATA(t4).
DATA(t43) = t4 - t3.
ASSERT result1 = result2.
cl_demo_output=>display( |Optimization factor: { t21 / t43 }| ).
Description
This example demonstrates how a string chaining is optimized when the right side of an
assignment extends a string by appending content.
In this case, the strings are processed in a loop effected by a REDUCE
expression.
- There is no optimization in the first
REDUCE
expression since an expression is appended to the strings
.
- In the second
REDUCE
expression, the expression is assigned to an helper variablenum
usingLET
. This variable can be appended tos
directly without a subtotal needing to be produced.
Without optimization the runtime increases quadratically with the number of iterations, which has a noticeable effect for large numbers n
.