Created
June 23, 2014 17:19
-
-
Save vighneshbirodkar/dd8f7d7fa97ab78bfa71 to your computer and use it in GitHub Desktop.
Comapring Numpy vs Python to find maximum
This file contains hidden or 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
import random | |
import numpy as np | |
import time | |
class ABC: | |
def __init__(self,x): | |
self.prop = x | |
def __lt__(self, other): | |
return self.prop < other.prop | |
def __le__(self, other): | |
return self.prop <= other.prop | |
def __eq__(self, other): | |
return self.prop == other.prop | |
def __ne__(self, other): | |
return self.prop != other.prop | |
def __gt__(self, other): | |
return self.prop > other.prop | |
def __ge__(self, other): | |
return self.prop > other.prop | |
N = 1000000 | |
obj_list = [ABC(random.randint(1,N)) for i in range(N)] | |
t = time.time() | |
m = max(obj_list, key = lambda x : x.prop) | |
print m.prop | |
print "Python took",time.time() - t,"secs" | |
a = np.array(obj_list) | |
t = time.time() | |
print a.max().prop | |
print "Numpy took",time.time() - t,"secs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment