Created
September 28, 2021 01:15
-
-
Save pwberrett/4a71ad863fe905e66dc316ebe2f02d23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 ia thw 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)) | |
runfile('C:/A/untitled7.py', wdir='C:/A') | |
[ True False False False True False False False True False] | |
[ True False False False True False False False True False] | |
[ True False False False True False False False True False] | |
time taken = 0.5050010681152344 | |
[ True False False False True False False False True False] | |
[ True False False False True False False False True False] | |
[ True False False False True False False False True False] | |
time taken = 2.4599974155426025 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment