N = [1, 2, 3, nil, nil] P nn1 = n. compactp n1n2 = n. Compact! P n2p n
It mainly involves communication with limited levels. I like research and I am sorry for it.
Let's talk about an array loop first.
Arr = [1, 2, 3] n = arr <arrp n output: [1, 2, 3, [...]
Analysis:
<The input is an arr pointer #=> [1, 2, 3, arr]
In fact, this is a loop. The actual values are: [1, 2, 3, [1, 2, 3, [1, 2, 3, 2, 3,...]
Arr [3] = arr
Arr [3] [3] = arr
Common Ruby array methods:
1. At Method
Arr = [1, 2, 3] P arr. At (2) P arr [2]
In fact, at is more efficient than the [] method because it does not accept the range parameter.
Test:
Require 'benchmark' n = (1 .. 1000000 ). to_al = n. lengthbenchmark. BM do | BM. report ("at") Do I = 0 while I <L n. at (I) I + = 1 end BM. report ("[]") Do I = 0 while I <l n [I] I + = 1 end output: User System Total realat 0.610000 0.000000 0.610000 (0.609000) [] 0.625000 0.000000 0.625000 (0.625000)
Conclusion: at is the first choice for retrieving elements from large arrays.
2. Compact Delete Method
N = [1, 2, 3, nil, nil] P nn1 = n. compactp n1n2 = n. Compact! P n2p n
Delete the NIL element in the array,! Returns the array itself. If the array does not contain nil, it does not contain! Return nil,! Returns the array itself.
N = [1, 2, 3, nil, nil] P n. Delete (NiL) P n
The delete method can also delete all nil elements to compare the performance:
Require 'benchmark' n = array. new (100000000, nil) n1 = array. new (100000000, nil) benchmark. BM do | BM. report ("delete") Do n. delete (NiL) end BM. report ("Compact") Do n1.compact End User System Total realdelete 0.718000 0.016000 0.734000 (0.735000) compact 1.360000 0.062000 1.422000 (1.453000)
Conclusion: delete is the preferred choice for deleting elements.
3. For each while
Require 'benchmark' n = (1 .. 100000 ). to_abenchmark.bm do | BM. report ("each") Do n. each do | d end BM. report ("for") Do For I in n I end BM. report ("while") Do I = 0 while I <n. length N [I] I + = 1 end endend user system total realeach 0.015000 0.000000 0.015000 (0.015000) for 0.016000 0.000000 0.016000 (0.016000) while 0.078000 0.000000 0.078000 (0.078000)
Conclusion: Each is very efficient.
4. Flatten Method
N = [, 3] n1 = [, 6] n2 = [N1, N] P n2p n2.flatten # [[4, 5, 6], [1, 2, 3] [4, 5, 6, 1, 2, 3]
5. Comparison between sort and custom methods
Require 'benchmark' n = (1 .. 100 ). to_an1 = (23 .. 89 ). to_an2 = (900 .. 3003 ). to_an = n2 + N1 + N # The custom method is def bubble_sort (ARR) 1. upto (ARR. length-1) Do | I | (ARR. length-I ). times do | j | if arr [J]> arr [J + 1] arr [J], arr [J + 1] = arr [J + 1], arr [J] end arrendbenchmark. BM do | BM. report ("sort") Do n. sort end BM. report ("personal") Do bubble_sort (n) End User System Total realsort 0.000000 0.000000 0.000000 (0.000000) Personal 3.109000 0.109000 3.218000 (3.220000)
Conclusion: The built-in Ruby method is still very powerful. The preferred method is sort.
6. uniq Method
If you delete the repeated elements of the array, the test will not happen, similar to the above sort.