Skip to content

Instantly share code, notes, and snippets.

@pwberrett
Created September 28, 2021 01:13
Show Gist options
  • Save pwberrett/a261e26ea8254d39b9b61c913ffcf178 to your computer and use it in GitHub Desktop.
Save pwberrett/a261e26ea8254d39b9b61c913ffcf178 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 28 10:13:27 2021
@author: pberr
"""
import numpy as np
import time
mylist=[]
for i in range (0,700):
mylist.append(i)
myarray = np.empty((600, 600))
for x in range (0,600):
for y in range (0,600):
p=y+(x*0.25)
myarray[y,x]=p
myarray=np.asarray(myarray)
# object here is to produce an array of 600 x 600 with True/False values depending
# on whether elements in myarray are in mylist.
# The question is - what si teh fastest way to do so?
# using isin (loop of 10000 used for timing illustration)
start = time.time()
for i in range (0,10):
s1=np.isin(myarray,mylist)
end = time.time()
print (s1[0][0:10])
print (s1[1][0:10])
print (s1[2][0:10])
print("time taken = ",(end-start))
def quickisin(array,mylist):
mylist=set(mylist)
p=[]
t=len(array)
for j in range (0,t):
h=array[j]
s1 = [item in mylist for item in h]
p.append(s1)
q=np.asarray(p)
return q
start = time.time()
for i in range (0,10):
s1=quickisin(myarray,mylist)
end = time.time()
print (s1[0][0:10])
print (s1[1][0:10])
print (s1[2][0:10])
print("time taken = ",(end-start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment