mikekestemont / copia

Bias correction for richness in abundance data
Creative Commons Zero v1.0 Universal
9 stars 1 forks source link

Boolean counting #9

Closed fbkarsdorp closed 3 years ago

fbkarsdorp commented 3 years ago

Currently we use two ways to count the number of true elements in a NumPy array:

https://github.com/mikekestemont/copia/blob/47c88046071a7121f2cfe8e49b193ceb9019e947/copia/richness.py#L32

and

https://github.com/mikekestemont/copia/blob/47c88046071a7121f2cfe8e49b193ceb9019e947/copia/richness.py#L79

The first one using indexing is about twice as fast as the second one. However, an even faster solution is to use something like the following:

x = np.random.randint(0, 100, 1000)
np.count_nonzero(x > 0)