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.
- Sorted Non Unique
- Sorted Unique
- 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 < 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( ).</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.


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 Take – Free 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
- ABAP on SAP HANA. Part I. First Program in ABAP HANA
- ABAP on SAP HANA. Part II. ADT Eclipse and HANA Studio
- ABAP on SAP HANA. Part III. Debugging in ADT
- CDS Part 1. Core Data Services – Introduction
- CDS Part 2. Core Data Services – Deep Dive
- ABAP on SAP HANA. Part VI. New Age Open SQL ABAP 740
- ABAP on SAP HANA. Part VII. SQL Script and SAP HANA Stored Procedure
- ABAP on SAP HANA. Part VIII. ADBC – ABAP DataBase Connectivity
- ABAP on SAP HANA. Part IX. AMDP – ABAP Managed Database Procedure
- ABAP on SAP HANA. Part X. AMDP with SELECT OPTIONS
- ABAP on SAP HANA. Part XI. Are Native SQL and Open SQL Competitors?
- ABAP on SAP HANA. Part XII. Open SQL, CDS or AMDP, which Code to Data Technique to use?
- ABAP on SAP HANA. Part XIII. Sample Functional Specification of HANA Project
- ABAP on SAP HANA: Part XIV. HANA Ready, HANA-tization & HANA Plus
- CDS Part 3. Expose CDS Views as OData Service through Annotation
- ABAP on SAP HANA: Part XVI. HANAtization
- ABAP on SAP HANA: Part XVII. ATC – ABAP Test Cockpit Setup & Exemption Process
- SAP ABAP on HANA: Part XVIII. SALV IDA (Integrated Data Access)
- ABAP for SAP HANA. Part XIX. Sample Technical Specification of HANA Project
- ABAP for SAP HANA. Part XX. ALV Report On SAP HANA – Opportunities And Challenges
- 4 Useful Tips on ABAP and ABAP on HANA
- Associations in HANA – A Conceptual Approach
- CDS Part 5. ABAP CDS Views With Authorization
- ABAP for SAP HANA. Part XXII. How to Consume Native HANA Views Using ADBC?
- CDS Part 6. Basic Expressions & Operations Available for CDS View – I
- 115 ABAP for SAP HANA Interview Questions & Answers
- CDS Part 7. Basic Expressions & Operations Available for CDS View – II
- CDS Part 8. Usage of Built-In Functions in CDS – I
- CDS Part 9. Usage of Built-In Functions in CDS – II
- CDS Part 10. Usage of Built-In Functions in CDS – III
- CDS Part 11. How to Consume CDS View in Smart Business Service KPI Fiori Apps?
- CDS Part 12. Useful 6 CDS Related Tools in ADT
- CDS Part 13. Key Definition in CDS Views
- CDS Part 14. ABAP Annotations for Translatable Texts in CDS Views
- CDS Part 15. Associations in CDS Views – I
- ABAP for SAP HANA – Part 23. How to Access Database Schema Dynamically Using AMDP?
- Video Course – New Features and Syntaxes in SAP ABAP 7.40+ with Exercises
- CDS Part 16. Usage of Built-In Functions in CDS – IV
- ‘ABAP for SAP HANA’ Points to Remember
- Limitations of Open SQL Compared to SQL Script in HANA
- CDS Part 17. How to Overcome GUID Mismatch Linking Problem in ABAP CDS?
- Curious Case of ATC – Priority 2 – Low Performance Operations on Internal Tables
- ABAP for SAP HANA – Part 24. Custom Report Using AMDP