Skip to content

Instantly share code, notes, and snippets.

View StrikingLoo's full-sized avatar
😄

Luciano StrikingLoo

😄
View GitHub Profile
import sys
a_list = [1 for i in range(10000000) ]
a_generator = (1 for i in range(10000000) )
sys.getsizeof(a_list) # 81528064 Bytes
sys.getsizeof(a_generator) # 80 Bytes
gen = (1 for _ in range(1000) )
# iterating manually
while True:
try:
my_value = gen.next()
do_stuff_with(my_value)
except:
break
with open('big.csv') as f:
for line in f:
process(line)
# regular, imperative declaration.
def twice(x):
return 2*x
twice(5) # 10
# declaration assigning a lambda
#(bad practice, don't try this at home!)
twice = lambda x: 2*x
twice(x) #10
def triple(a):
return 3*a
thrice = lambda x: 3*x
these = [triple(i) for i in range(5) ]
are = [(lambda x: 3*x)(i) for i in range(5) ]
all = [thrice(i) for i in range(5) ]
the = map(thrice, range(5))
same = map(triple, range(5))
some_list = list(range(5))
only_evens = filter(lambda x: x%2 == 0, some_list)
only_evens_list = [x for x in some_list if x%2==0]
# list(only_evens) == only_evens_list
import time
BIG = 20000000
def f(k):
return 2*k
def benchmark(function, function_name):
start = time.time()
function()
def list_a():
list_a = []
for i in range(BIG):
list_a.append(f(i))
def list_b():
list_b = [f(i) for i in range(BIG)]
def list_c():
list_c = map(f, range(BIG))
def f(x):
return 2*x
a = list( map(f,[1,2,3]) )
b = []
for i in [1,2,3]:
b.append(f(i))
a == b #True
def imperative_linear_search(some_list,some_element):
for e in some_list:
if e == some_element:
return True
return False
def recursive_linear_search(some_list,some_element):
if some_list == []:
return False
elif some_list[0]==some_element: #first element always exists at this point