Skip to content

Instantly share code, notes, and snippets.

@alezhu
Last active March 4, 2018 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alezhu/79ca454e64dfb39e702062d3edf2408e to your computer and use it in GitHub Desktop.
Save alezhu/79ca454e64dfb39e702062d3edf2408e to your computer and use it in GitHub Desktop.
REPORT ZTEST_SORT_DUP_DELETE_VS_HASH.
TYPES:
BEGIN OF ts_data,
num TYPE i,
END OF ts_data.
TYPES:
BEGIN OF ts_result,
lines TYPE i,
times TYPE i,
alg TYPE i,
diff TYPE i,
END OF ts_result,
tt_result TYPE STANDARD TABLE OF ts_result WITH DEFAULT KEY.
DATA gt_result TYPE tt_result.
DATA lo_rand TYPE REF TO cl_abap_random.
START-OF-SELECTION.
lo_rand = cl_abap_random=>create(
* seed = seed
).
FREE gt_result.
DATA(lv_count) = 1.
WHILE lv_count <= 100000.
DATA(lv_times) = 1.
WHILE lv_times <= 1000000 AND ( lv_times DIV 10 ) * lv_count <= 100000000.
PERFORM test USING lv_times lv_count CHANGING gt_result.
MULTIPLY lv_times BY 10.
ENDWHILE.
MULTIPLY lv_count BY 10.
ENDWHILE.
LOOP AT gt_result REFERENCE INTO DATA(lps_result).
AT NEW lines.
ULINE.
ENDAT.
WRITE: lps_result->times, lps_result->lines, SWITCH #( lps_result->alg WHEN 1 THEN 'Sort+Delete:' ELSE 'Hash:' ), lps_result->diff .
NEW-LINE.
ENDLOOP.
FORM test USING iv_times TYPE i iv_lines TYPE i CHANGING ct_result TYPE tt_result.
DATA lt_source TYPE STANDARD TABLE OF ts_data WITH DEFAULT KEY.
DATA lt_data TYPE STANDARD TABLE OF ts_data WITH DEFAULT KEY.
DATA lt_datah TYPE HASHED TABLE OF ts_data WITH UNIQUE KEY num.
DO iv_lines TIMES.
APPEND VALUE #( num = lo_rand->int( ) MOD 10 ) TO lt_source.
ENDDO.
DO.
GET RUN TIME FIELD DATA(lv_start).
DATA(lv_type) = sy-index.
CASE sy-index.
WHEN 1.
DO iv_times TIMES.
FREE lt_data.
LOOP AT lt_source ASSIGNING FIELD-SYMBOL(<s_data>).
APPEND <s_data> TO lt_data.
ENDLOOP.
SORT lt_data BY num.
DELETE ADJACENT DUPLICATES FROM lt_data COMPARING num.
ENDDO.
WHEN 2.
DO iv_times TIMES.
FREE lt_datah.
LOOP AT lt_source ASSIGNING <s_data>.
INSERT <s_data> INTO TABLE lt_datah.
ENDLOOP.
ENDDO.
WHEN OTHERS.
EXIT.
ENDCASE.
GET RUN TIME FIELD DATA(lv_end).
DATA(lv_diff) = lv_end - lv_start.
APPEND VALUE #( times = iv_times lines = iv_lines alg = lv_type diff = lv_diff ) TO ct_result.
ENDDO.
ENDFORM.
@aragorn888
Copy link

pay attention to the SORT gt_data. it will not work in this way .
the sort isn't done , and the delete adjacent respectively .
you can see in debug how your table gt_data it's unsorted after SORT.
you should put : sort by num, or create table not with default key but with : WITH KEY num.

@alezhu
Copy link
Author

alezhu commented Mar 4, 2018

pay attention to the SORT gt_data. it will not work in this way .
the sort isn't done , and the delete adjacent respectively .
you can see in debug how your table gt_data it's unsorted after SORT.

you're absolutely so damn right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment