5

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

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
phil soady
  • 11,043
  • 5
  • 50
  • 95

1 Answers1

6

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%

gkubed
  • 1,849
  • 3
  • 32
  • 45
  • 1
    useful, thanks gkubed. The info (real solution) lies in the blog from Horst Keller that VWegert linked above. – phil soady Jul 20 '17 at 16:52