I'm looking for a faster way to add to an existing string in ABAP.
ABAP version NW 7.x
Currently the string concatenation takes a very long time, especially once the string gets large. We need to build strings that are > 10 mb.
The string is constructed logically with many x = x + y
operations.
concatenate l_main_string l_extra into l_main_string. " very slow
Is there a faster way to construct large strings in ABAP? Perhaps Kernel tool we can call?
EDIT: Based on Feedback and answer posted.
Most useful is the blog Linked by VWegert. Is really the answer to the question.
It also seems the key issue is older kernels out customers are using.
The && and Concatenate where very similar on our system. (NW 7.50) 1.46s vs 1.50s repsectively. Clearly the same optimisation was running and it works in a acceptable way.
METHOD PERF_TEST_STRING. " 1.50 sec
DATA: n TYPE i value 5000000,
lv_string TYPE string.
do n times.
CONCATENATE lv_string 'ABCDEFGHIJ0123456789' into lv_string.
ENDDO.
r_len = strlen( lv_string ).
ENDMETHOD.
METHOD PERF_TEST_STRING2. "1.46
DATA: n TYPE i value 5000000,
lv_string TYPE string.
do n times.
lv_string = lv_string && 'ABCDEFGHIJ0123456789'.
ENDDO.
r_len = strlen( lv_string ).
ENDMETHOD.
So i'm off to check kernel level of customer and look for another reason why things are slow.
BTW: I CANT use
x = x && Y. " doesnt work prior to < 7.20
since many of customers dont have >=7.20 :(
If you need to concatenate inside the loop, it is always suggested to use StringBuilder. For simple string concatenation, you need not use StringBuilder , java compiler will do the trick for you. However, if you need to concatenate inside a loop, you need to manually apply StringBuilder.
Like internal tables, strings are dynamic data objects. The corresponding built-in ABAP types are string and xstring. These are the dynamic equivalent of the data types c and x, which define text fields or byte fields of fixed length. Text strings and text fields contain character strings.
To concatenate rows in an internal table, the predefined function concat_lines_of can be used. The ABAP runtime environment executes an internal optimization to reduce reallocations if new fragments are attached to an existing string within a loop.
In order to find the length of character strings, we can use STRLEN statement. The STRLEN () function returns the number of characters contained in the string.
Use the following format:
l_main_string = |{ l_main_string }{ l_extra }|.
I performed two tests - one that concatenated a ten character string to itself 50,000 times to test small additions, and another that added the string to itself 26 times to test large additions (this gets very large very quickly).
Small string concatenation test
DO 15 TIMES.
lv_string = '1234567890'.
DO 50000 TIMES.
CONCATENATE '1234567890' lv_string INTO lv_string.
ENDDO.
ENDDO.
Runtime: 775778ms (51718ms average per run).
DO 15 TIMES.
lv_string = '1234567890'.
DO 50000 TIMES.
lv_string = |1234567890{ lv_string }|.
ENDDO.
ENDDO.
Runtime: 100543ms (6702ms average per run).
Performance gain over CONCATENATE
: 672%
Large string concatenation test
DO 15 TIMES.
lv_string = '1234567890'.
DO 26 TIMES.
CONCATENATE lv_string lv_string INTO lv_string.
ENDDO.
ENDDO.
Runtime: 143116ms (9541ms average per run).
DO 15 TIMES.
lv_string = '1234567890'.
DO 26 TIMES.
lv_string = |{ lv_string }{ lv_string }|.
ENDDO.
ENDDO.
Runtime: 51995 (3466ms average per run).
Performance gain over CONCATENATE
: 175%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With