cancel
Showing results for 
Search instead for 
Did you mean: 

Can ABAP optimize inequalities in LOOP AT sortedItab WHERE, like greater than, between?

Sandra_Rossi
Active Contributor
0 Kudos
122

Do you know if the below LOOP AT will be optimized and fast?

CLASS ltc_test DEFINITION
  FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.

  PRIVATE SECTION.
    METHODS test FOR TESTING.

    TYPES ty_itab TYPE SORTED TABLE OF i WITH NON-UNIQUE KEY table_line.

    CLASS-DATA itab TYPE ty_itab.

    CLASS-METHODS class_setup.
ENDCLASS.


CLASS ltc_test IMPLEMENTATION.
  METHOD class_setup.
    DATA(integer_generator) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
                                                          min  = 1
                                                          max  = 1000000 ).
    itab = VALUE #( FOR i = 1 WHILE i <= 1000000
                    ( integer_generator->get_next( ) ) ).
  ENDMETHOD.

  METHOD test.
    DO 20 TIMES.
      LOOP AT itab TRANSPORTING NO FIELDS
           WHERE table_line BETWEEN 250000 AND 300000.
      ENDLOOP.
    ENDDO.
  ENDMETHOD.
ENDCLASS.

Thanks

Sandra

Accepted Solutions (1)

Accepted Solutions (1)

Sandra_Rossi
Active Contributor

The optimization of LOOP AT has been improved in ABAP 7.58 to support BETWEEN, >, >=, <, <=.

EDIT May 11th, 2025, concerning ABAP 7.58:

  1. These conditions are not optimized: inequality (<>), CP "value*", "IN range" for BT, GT, GE, LT, LE.
  2. It's not said whether comp+0(1) = "A" is optimized or not, but "comp BETWEEN 'A' and 'AZZZZZZZ' " may be used instead. I don't know if "comp >= 'A' and comp < 'B' " is optimized (if anybody can check, answer welcome).

Before ABAP 7.58, the optimization used to work only with = and AND, so BETWEEN was not optimized.

References of WHERE optimization in ABAP Keyword Documentation:

EDIT: for information, I did a performance measurement on two systems, average on several runs:

  • ABAP 7.40 : 2.2 seconds
  • ABAP 7.58 : 0.2 seconds

To make sure that the "CPU" speed is the same on both systems, I could verify that the average speed is the same for the below adaptation (WHERE with "=" should run identically in both ABAP systems), which is around 1.3 seconds:

    DO 1000000 TIMES.
      LOOP AT itab TRANSPORTING NO FIELDS
           WHERE table_line = 250000.

 

Answers (1)

Answers (1)

Sandra_Rossi
Active Contributor
0 Kudos

(deleted)