Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAP Stringbuilder for ABAP?

Tags:

string

abap

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 :(

like image 978
phil soady Avatar asked Jul 20 '17 12:07

phil soady


People also ask

Is StringBuilder still necessary?

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.

What is the data type for string in SAP ABAP?

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.

How do you concatenate in ABAP?

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.

How do you find the length of a string in ABAP?

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.


1 Answers

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%

like image 195
gkubed Avatar answered Sep 18 '22 17:09

gkubed