Python Benchmark: Thresholding, numpy vs OpenCV

Looking for a fast way to "Thresholding, numpy vs OpenCV" 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

OpenCV

139 us

7.17K op/s

Speed

BenchmarkRuntime
Operations
VS
Visual
OpenCV139 us7.17K op/s0.00013940497523262386
numpy.where - uint86.05 ms165 op/s43×0.006050025035316746
numpy.where7.66 ms131 op/s55×0.007661474247773488
Numpy indexing9.80 ms102 op/s70×0.009802322648465634

Memory

BenchmarkPeak Memory
Score
VS
Visual
Numpy indexing1.11 MB6.961108
OpenCV1.55 MB8.671.4×1548
numpy.where - uint82.17 MB6.881.95×2168
numpy.where9.36 MB6.149356

Output

Numpy indexing
127.4941635131836
OpenCV
124.86409664154053
numpy.where
125.00295639038086
numpy.where - uint8
124.92027282714844