In [1]:
%load_ext cython
In [2]:
%%cython
cpdef set f(list x):
""" Convert a list to a set. """
return set(x)
In [3]:
# This is a list: good!
data = [1,2,3]
print(f(data))
In [4]:
# Hey, this isn't a list!
data = (1,2,3)
print(f(data))
In [5]:
%%cython
def f(x):
cdef list output
output = x
return output
In [6]:
# Succeeds if we really did give a list
f([])
Out[6]:
In [7]:
# Fails if we don't
f(42)
In [8]:
%%cython
def f(x, n):
for i in range(n):
x.append(i)
def g(list x, n):
for i in range(n):
x.append(i)
In [9]:
%timeit f([], 1000000)
%timeit g([], 1000000)
In [10]:
%%cython
def h(x, n):
for i in range(n):
x.add(i)
return x
def k(set x, n):
for i in range(n):
x.add(i)
return x
In [11]:
%timeit h(set(), 1000000)
%timeit k(set(), 1000000)
In [ ]: