Quantcast
Channel: Tweaks
Viewing all articles
Browse latest Browse all 66

SORTing Algorithm – Performance Comparision

$
0
0

In one of our previous articles, we talked about the SORTing Algorithm logic which is asked in many Tier One MNCs. We showed few ways to achieve it. After going through the different SORTing method, our friend Stephan Koester from Koester-Consulting was queries to validate the performance of the different SORTs.

So, he wrote one program using the same code snippets and tried to determine which SORTing Algorithm is the most optimized one. The finding is quite interesting.

For the experiment, Stephan created the tables with rows 2 to the power n where n starting from 1 till 13. In short, the table had row counts 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192.

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

To test the performance better, he created two tables. One in ascending order from 2 till 2 to the power of 13. And the second table in descending order from 2 to the power of 13 till 2.

Techniques Used

If you check the code below, he used 3 methods to Sort and record the time.

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

You might also Like Our Experiment with DELETING in LOOP

Program with 3 SORT Algorithm

<code>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 &lt; lv_len.
    DATA(lv_min) = it_three[ i ].
    DATA(j) = i + 1.
    WHILE j &lt; lv_len + 1 .
      IF it_three[ j ] &lt; 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( ).</code>

Result

Let us run and check the time taken for each Sort Algorithm

As expected, the Manual Sorting algorithm took exponential time compared to the standard syntax. The SORTED_UNIQUE fared better than SORTED_NONUNIQUE. Although the time difference is quite minuscule but SORTED UNIQUE technique was consistently better than SORTED NON UNIQUE and with more volume of data, the gap between UNIQUE and NON UNIQUE would be more visible and impactful.

Another Experiment Why is PER in conditions not always 1 ?

Keyword – DISCARDING DUPLICATES

We already mentioned about the new key word DISCARDING in previous article. It is available from NW7.51. Check below, NW 7.4 does not recognize it.

SAP Netweaver 740
DISCARDING keyword available after NW751

Keywords – CORRESPONDING, BASE, DISCARDING DUPLICATES

If you need a reference code snippet to populate table with Non Unique and Unique data, check the below program provided by Stephan. He has created two internal tables and then inserted data of both the tables into one. One of the final table is SORTED UNIQUE and the other one is SORTED NON UNIQUE. This is a good example for everyone to learn modern Syntaxes and be more efficient at their work place.

<code>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( ).</code>

Output

Also TakeFree Video Course on New Syntax in ABAP 7.4+

We would like to thank Stephan for all his time and effort in providing us the code snippet and the screenshots. You may connect with Stephan at LinkedIn or visit his company website KCO for any SAP Consulting Work.

Your Feedback Please..

5300+ SAP Technical Practitioners from 6 Continents of the SAP World. Install Telegram app and click this link to join – Telegram SAP Technical Discuss Group. 

Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.

Check HANA-ABAP Tutorials


Viewing all articles
Browse latest Browse all 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>