Author Topic: Array_Intersection  (Read 2723 times)

0 Members and 1 Guest are viewing this topic.

Offline René

  • Newbie
  • Posts: 2
    • View Profile
Array_Intersection
« on: March 04, 2019, 10:43:03 am »
I am trying to find the best way to write a subroutine of Array Intersection

I tried 3 methods.
- a sequential method (18 seconds)
- a dichotomous search method  ( 9 seconds)
-and finally I learn or I try to understand how to use the SMcNeill tools and so I wrote a routine with the use of _MEM. It's difficult for me because I do not understand all the subtleties of the English language and even less those of the language "Basic"  (12 seconds)

These tables are already sorted
I thought the 3rd method (with _Mem) would be faster, but no.
I think I have to make mistakes in using these instructions

Thank you in advance for your advice


Code: QB64: [Select]
  1. '$INCLUDE:'SET.BI'
  2.  
  3. DIM a(100000, 20) AS LONG
  4. DIM b(10) AS LONG
  5. DIM c(20) AS LONG
  6. DIM result(100) AS LONG
  7.  
  8. Read_tables
  9.  
  10. DIM ma AS _MEM: ma = _MEM(a())
  11. DIM mb AS _MEM: mb = _MEM(b())
  12. DIM mc AS _MEM: mc = _MEM(c())
  13. 'MemSortSome ma, 0, 19
  14. 'MemSortSome mb, 0, 9
  15.  
  16. Intersec_sequentiel:
  17.  
  18. REM calcul intersection_Array methode sequentielle
  19. PRINT "1 - Intersection_array  methode sequentielle"
  20. PRINT "wait about 20 seconds...."
  21. FOR k = 0 TO 10: result(k) = 0: NEXT k
  22. temps1 = TIMER(0.001)
  23. commun# = 0
  24. repet = 0
  25.     k = 0: ll% = 0
  26.     DO
  27.         i = 0
  28.         DO
  29.             j = 0
  30.             DO
  31.                 IF a(k, j) > b(i) THEN EXIT DO
  32.                 IF b(i) = a(k, j) THEN commun# = commun# + 1: ll% = ll% + 1
  33.                 j = j + 1
  34.             LOOP UNTIL j > 19
  35.             i = i + 1
  36.         LOOP UNTIL i > 9
  37.         result(ll%) = result(ll%) + 1
  38.         ll% = 0
  39.         k = k + 1
  40.     LOOP UNTIL k > 19
  41.     repet = repet + 1
  42. LOOP UNTIL repet > 100000
  43. temps1 = TIMER(0.001) - temps1
  44. PRINT "Commun="; commun#, "temps="; temps1
  45. FOR k = 0 TO 10: PRINT k; "="; result(k): NEXT k
  46. PRINT "Press a Key to continue"
  47.  
  48.  
  49.  
  50.  
  51. Intersec_dicho:
  52.  
  53. FOR k = 0 TO 10: result(k) = 0: NEXT k
  54. PRINT "2 - Calcul Array_Intersection dichotomie "
  55. PRINT "wait about 20 seconds...."
  56. temps6 = TIMER(0.001)
  57. commun# = 0
  58. repet = 0
  59.     k = 0: ll% = 0
  60.     DO
  61.         FOR i1 = 0 TO 19: c(i1) = a(k, i1): NEXT i1
  62.         'MemSortSome mc, 0, 19  (gain 9 seconds)
  63.         i = 0
  64.         DO
  65.             inf% = 0: sup% = 19
  66.             RechercheCombi:
  67.             IF inf% > sup% THEN GOTO existepas
  68.             mil% = INT((inf% + sup%) / 2)
  69.             IF b(i) = c(mil%) THEN commun# = commun# + 1: ll% = ll% + 1: GOTO existepas
  70.             IF b(i) < c(mil%) THEN sup% = mil% - 1: GOTO RechercheCombi
  71.             IF b(i) > c(mil%) THEN inf% = mil% + 1: GOTO RechercheCombi
  72.             existepas:
  73.             i = i + 1
  74.         LOOP UNTIL i > 9
  75.         result(ll%) = result(ll%) + 1
  76.         ll% = 0
  77.         k = k + 1
  78.     LOOP UNTIL k > 19
  79.     repet = repet + 1
  80. LOOP UNTIL repet > 100000
  81. temps6 = TIMER(0.001) - temps6
  82. PRINT "Commun="; commun#, "temps="; temps6
  83. FOR k = 0 TO 10: PRINT k; "="; result(k): NEXT k
  84. PRINT "Press a Key to continue"; ";"
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. intersec_mem:
  92.  
  93. FOR k = 0 TO 10: result(k) = 0: NEXT k
  94. REM calcul intersection tableau avec _mem
  95. PRINT "Calcul Array_Intersection with _mem "
  96. PRINT "wait about 20 seconds...."
  97. commun# = 0
  98. repet = 0
  99. temps8 = TIMER(0.001)
  100.     k = 0: ll% = 0
  101.     DO
  102.         FOR i1 = 0 TO 19: c(i1) = a(k, i1): NEXT i1
  103.         'MemSortSome mc, 0, 19 (gain 9 seconds)
  104.         kk% = 0
  105.         DO
  106.             index% = BinaryMemSearchSome(b(kk%), mc, 0, 19)
  107.             IF index% <> -1 THEN commun# = commun# + 1: ll% = ll% + 1
  108.             kk% = kk% + 1
  109.         LOOP UNTIL kk% > 9
  110.         result(ll%) = result(ll%) + 1
  111.         ll% = 0
  112.         k = k + 1
  113.     LOOP UNTIL k > 19
  114.     repet = repet + 1
  115. LOOP UNTIL repet > 100000
  116. temps8 = TIMER(0.001) - temps8
  117. PRINT "Commun="; commun#, "temps="; temps8
  118. FOR k = 0 TO 10: PRINT k; "="; result(k): NEXT k
  119.  
  120.  
  121.  
  122.  
  123. SUB Read_tables
  124.     SHARED a() AS LONG, b() AS LONG
  125.  
  126.     FOR i = 0 TO 19
  127.         FOR j = 0 TO 19
  128.             READ a(i, j)
  129.         NEXT j
  130.     NEXT i
  131.     FOR j = 0 TO 9
  132.         READ b(j)
  133.     NEXT j
  134.     CLOSE
  135.  
  136.  
  137.  
  138. DATA 1,2,3,5,9,11,13,14,24,27,32,45,47,58,60,64,65,67,69,70
  139. DATA 2,4,5,7,11,12,22,23,27,28,31,41,44,46,48,53,54,62,66,70
  140. DATA 3,5,9,16,21,24,28,33,36,37,39,42,43,44,46,53,57,60,65,70
  141. DATA 5,7,10,17,26,28,32,33,35,36,39,42,44,46,49,52,57,58,60,68
  142. DATA 3,7,8,9,11,12,18,22,26,29,30,33,40,46,48,49,55,58,60,63
  143. DATA 1,2,6,10,11,16,17,21,27,35,42,45,49,56,59,63,66,68,69,70
  144. DATA 4,13,14,23,26,29,36,37,39,41,42,45,46,52,53,54,56,61,63,65
  145. DATA 5,9,11,15,19,21,22,25,30,36,38,41,42,45,56,57,60,65,66,68
  146. DATA 5,7,8,12,16,24,25,26,28,32,33,41,42,43,48,50,52,57,62,65
  147. DATA 3,11,15,16,20,21,33,34,47,49,50,51,52,54,57,58,61,62,66,70
  148.  
  149. DATA 2,8,9,10,13,18,19,22,23,24,28,33,34,37,41,42,43,45,51,61
  150. DATA 2,4,7,11,12,14,15,16,28,36,38,40,44,45,56,63,65,67,68,70
  151. DATA 7,9,12,13,15,19,21,29,30,31,34,36,37,39,41,48,53,62,64,68
  152. DATA 3,5,10,13,14,15,16,22,26,29,30,34,36,41,43,47,48,55,66,67
  153. DATA 2,3,7,8,15,20,22,32,35,41,49,50,51,53,54,58,59,60,69,70
  154. DATA 1,6,10,14,20,21,23,24,25,27,32,36,37,38,47,53,56,59,62,63
  155. DATA 4,11,12,15,25,28,30,31,33,35,39,43,45,49,51,56,59,63,66,69
  156. DATA 1,4,6,7,8,10,15,24,27,33,34,35,37,38,46,50,58,67,68,70
  157. DATA 10,11,12,14,15,17,18,23,25,37,38,42,52,53,54,55,63,64,65,68
  158. DATA 1,2,5,7,15,16,19,23,24,27,33,38,39,47,54,59,60,62,64,65
  159. DATA 1,3,11,21,28,32,45,58,60,70
  160.  
  161.  
  162. '$INCLUDE:'SET.BM'
  163.  

  [ You are not allowed to view this attachment ]    [ You are not allowed to view this attachment ]    [ You are not allowed to view this attachment ]  
« Last Edit: March 04, 2019, 11:39:59 am by René »