SORTing Algorithm – Performance Comparision

In one of our past articles, we discussed the Arranging Calculation rationale which is asked in numerous Level One MNCs. We showed not many ways of accomplishing it. In the wake of going through the different Arranging strategy, our companion Stephan Koester from Koester-Counseling was questions to approve the exhibition of the various SORTs. In this will see SORTing Algorithm – Performance Comparision.

In this way, he thought of one program utilizing similar code bits and attempted to figure out which Arranging Calculation is the most advanced one. The finding is very fascinating.

For the examination, Stephan made the tables with columns 2 to the power n where n beginning from 1 till 13. So, the table had column counts 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192. Let’s go for practical on SORTing Algorithm – Performance Comparision.

Guinea Pig for the Experiment – i.e The Test Data

To test the presentation better, he made two tables. One in rising request from 2 till 2 to the force of 13. What’s more, the subsequent table in sliding request from 2 to the force of 13 till 2.

Techniques Used

Assuming you really take a look at the code beneath, he utilized 3 strategies to Sort and record the time.

  1. Sorted Non Unique
  2. Sorted Unique
  3. Manual Sort Algorithm

Program with 3 SORT Algorithm

REPORT zstkoes_sort_performance.
DATA:
  lv_time_first  TYPE timestampl,
  lv_time_second TYPE timestampl,
  lv_time_third  TYPE timestampl,
  lv_time_fourth TYPE timestampl,
  lv_time_fifth  TYPE timestampl,
  lv_time_sixth  TYPE timestampl.

DATA:
  BEGIN OF gs_time,
    count             TYPE i,
    sorted_unique     TYPE timestampl,
    sorted_nonunique  TYPE timestampl,
    manual_sort_logic TYPE timestampl,
  END OF gs_time,
  gt_times LIKE TABLE OF gs_time.

TYPES:
  tt_int TYPE TABLE OF i WITH EMPTY KEY,
  BEGIN OF gtys_integer,
    integer TYPE i,
  END OF gtys_integer,
  gtyt_interger                  TYPE TABLE OF gtys_integer WITH EMPTY KEY,
  gtyt_interger_sorted_unique    TYPE SORTED TABLE OF gtys_integer WITH UNIQUE KEY integer,
  gtyt_interger_sorted_nonunique TYPE SORTED TABLE OF gtys_integer WITH NON-UNIQUE KEY integer.

DATA:it_three TYPE tt_int.

DO 13 TIMES.
  DATA(lv_count) = 2 ** sy-index.
  DATA(lt_table1) = VALUE gtyt_interger( FOR count2 = 1 THEN count2 + 1 UNTIL count2 > lv_count ( integer = count2 ) ).
  DATA(lt_table2) = VALUE gtyt_interger( FOR count = lv_count THEN count - 1 UNTIL count = 0 ( integer = count ) ).

  IF sy-index EQ 2.
    cl_demo_output=>begin_section( title = |Sample table| ).
    cl_demo_output=>write_text( text = |All tables follow the same rule:{
                                        cl_abap_char_utilities=>newline }First table: 1 to count{
                                        cl_abap_char_utilities=>newline }Second table: count to 1| ).
    cl_demo_output=>write_data( value = lt_table1 name  = |First Table| ).
    cl_demo_output=>write_data( value = lt_table2 name  = |Second Table| ).
    cl_demo_output=>begin_section( title = 'Time needed' ).
  ENDIF.

* 1. Sorted Non Unique
  GET TIME STAMP FIELD lv_time_first.
  DATA(lt_table4) = CORRESPONDING gtyt_interger_sorted_nonunique( BASE ( lt_table1 ) lt_table2 ).
  GET TIME STAMP FIELD lv_time_second.

* 2. Sorted Unique
  GET TIME STAMP FIELD lv_time_third.

* DISCARDING DUPLICATES can be used with NW7.51 and higher
  DATA(lt_table3) = CORRESPONDING gtyt_interger_sorted_unique( lt_table1 DISCARDING DUPLICATES ).
  lt_table3 = CORRESPONDING #( BASE ( lt_table3 ) lt_table2 DISCARDING DUPLICATES ).
  GET TIME STAMP FIELD lv_time_fourth.

  DATA(it_one) = VALUE tt_int( FOR count2 = 1 THEN count2 + 1 UNTIL count2 > lv_count ( count2 ) ).
  DATA(it_two) = VALUE tt_int( FOR count = lv_count THEN count - 1 UNTIL count = 0 ( CONV #( count ) ) ).

  CLEAR: it_three.
  GET TIME STAMP FIELD lv_time_fifth.
  APPEND LINES OF it_one TO it_three.
  APPEND LINES OF it_two TO it_three.

* 3. Manual Sort Logic
  DATA(lv_len) = lines( it_three ).
  DATA(i) = 1.
  WHILE i < lv_len.
    DATA(lv_min) = it_three[ i ].
    DATA(j) = i + 1.
    WHILE j < lv_len + 1 .
      IF it_three[ j ] < lv_min.
        DATA(lv_temp) = lv_min.
        lv_min = it_three[ j ].
        it_three[ j ] = lv_temp.
      ENDIF.
      j = j + 1.
    ENDWHILE.
    it_three[ i ] = lv_min.
    i = i + 1.
  ENDWHILE.

  GET TIME STAMP FIELD lv_time_sixth.

* Populating the table with the time for the 3 techniques
  gt_times = VALUE #( BASE gt_times ( count = lv_count
                                      sorted_unique = lv_time_fourth - lv_time_third
                                      sorted_nonunique = lv_time_second - lv_time_first 
                                      manual_sort_logic = lv_time_sixth - lv_time_fifth ) ).
ENDDO.

cl_demo_output=>write_data( value = gt_times ).
cl_demo_output=>display( ).

Result

Allow us to run and check the time taken for each Sort Calculation

True to form, the Manual Arranging calculation took outstanding time contrasted with the standard punctuation. The SORTED_UNIQUE fared better compared to SORTED_NONUNIQUE. Albeit the time contrast is very tiny yet Arranged One of a kind procedure was reliably better compared to Arranged NON Interesting and with more volume of information, the hole among Special and NON Remarkable would be more noticeable and significant.

Keyword – DISCARDING DUPLICATES

We previously referenced about the new watchword Disposing of in past article. It is accessible from NW7.51. Check beneath, NW 7.4 doesn’t remember it.

SAP Netweaver 740
DISCARDING keyword available after NW751

Keywords – CORRESPONDING, BASE, DISCARDING DUPLICATES

In the event that you really want a reference code bit to populate table with Non One of a kind and Exceptional information, check the underneath program given by Stephan. He has made two inner tables and afterward embedded information of both the tables into one. One of the last table is Arranged One of a kind and the other one is Arranged NON Special. This is a genuine model for everybody to learn present day Sentence structures and be more proficient at their work place.

REPORT zstkoes_sort.
TYPES:
  BEGIN OF gtys_integer,
    integer TYPE i,
  END OF gtys_integer,
  gtyt_interger                  TYPE TABLE OF gtys_integer WITH EMPTY KEY,
  gtyt_interger_sorted_unique    TYPE SORTED TABLE OF gtys_integer WITH UNIQUE KEY integer,
  gtyt_interger_sorted_nonunique TYPE SORTED TABLE OF gtys_integer WITH NON-UNIQUE KEY integer.

DATA(lt_table1) = VALUE gtyt_interger( ( integer = 9 ) ( integer = 1 ) ( integer = 9 ) ).
DATA(lt_table2) = VALUE gtyt_interger( ( integer = 20 ) ( integer = 9 ) ( integer = 4 ) ).

* CORRESPONDING can be used with NW7.40PL2 and higher
DATA(lt_table4) = CORRESPONDING gtyt_interger_sorted_nonunique( BASE ( lt_table1 ) lt_table2 ).

* DISCARDING DUPLICATES can be used with NW7.51 and higher
DATA(lt_table3) = CORRESPONDING gtyt_interger_sorted_unique( lt_table1 DISCARDING DUPLICATES ).
lt_table3 = CORRESPONDING #( BASE ( lt_table3 ) lt_table2 DISCARDING DUPLICATES ).

cl_demo_output=>write_data( value = lt_table1 name  = |First Table| ).
cl_demo_output=>write_data( value = lt_table2 name  = |Second Table| ).
cl_demo_output=>write_data( value = lt_table3 name  = |Sorted Table with unique key| ).
cl_demo_output=>write_data( value = lt_table4 name  = |Sorted Table with non-unique key| ).

cl_demo_output=>display( ).

Output

YOU MAY LIKE THIS

What are SAP managed services?

Transaction to launch Fiori launchpad

SAP ABAP Training Institute in Pune, SAP ABAP Courses Online

X
WhatsApp WhatsApp us