Python Benchmark: Unique values in array

Looking for a fast way to "Unique values in array" in python? Check below to see see which method is the fastest and uses less memory.

Packages

List packages as it's done in a requirements.txt file. Learn more

Setup

Runs before the benchmark, and it is not measured. Good place to import libraries and prepare data. Learn more

Code 1

Code 2

Code 3

Code 4

Teardown

Runs after the benchmark, and it is not measured. Good place to print out results. Learn more

Results

Fastest

pandas.unique

1.22 ms

822 op/s

Speed

BenchmarkRuntime
Operations
VS
Visual
pandas.unique1.22 ms822 op/s0.0012159861494288888
pandas.unique - sorted1.25 ms801 op/s1.03×0.0012483375298010337
numpy.unique4.86 ms206 op/s0.004862794652581215
set24.3 ms41.2 op/s20×0.02425069222226739

Memory

BenchmarkPeak Memory
Score
VS
Visual
set< 1 KB9.620
pandas.unique - sorted544 KB8.17544×544
pandas.unique544 KB8.18544×544
numpy.unique1.10 MB7.271100×1100

Output

numpy.unique
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99]
pandas.unique
[25 72  6 93 70  3 24 71 81 21 65 60 50 11  2 55 92 41 87 45 39 90 30 61
 76 14 20 22 38 17 42  9 49 79  0 35 10 40  1 82 28 97 89 52 68 83 43  5
 73 58 48  8 67 75 31 94 57 59 74 98 66 16 86 51 53 78 23 15 69 19 99 18
 47 77 84 46 29 96 26 85 32 91 95 33 54 62 64 27 13  4 34 44 63 12 56 88
 37 80  7 36]
pandas.unique - sorted
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99]
set
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99]