Skip to content

Instantly share code, notes, and snippets.

@jirivrany
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirivrany/8951802 to your computer and use it in GitHub Desktop.
Save jirivrany/8951802 to your computer and use it in GitHub Desktop.
Python - map vs. list generator
'''
@author: Jiri Vrany
'''
import hashlib
def hash_word(word):
'''
returns sha512 hash
'''
mes = hashlib.sha512()
mes.update(word)
return mes.hexdigest()
def listcompr(oldlist):
'''
list comprehension
'''
return [hash_word(s) for s in oldlist]
def mapfce(oldlist):
'''
built in map
'''
return map(hash_word, oldlist)
def forloop(oldlist):
'''
classic for loop
'''
newlist = []
for word in oldlist:
newlist.append(hash_word(word))
return newlist
def time_function(fname, param, rep=10):
'''
benchmark routine
'''
print(fname)
call_name = "{}({})".format(fname, param)
setup_name = "from __main__ import {}".format(fname)
print(timeit.timeit(call_name, setup=setup_name, number=rep))
if __name__ == '__main__':
import timeit
TXT = '''
Python is a programming language that lets you work more quickly
and integrate your systems more effectively. You can learn to use
Python and see almost immediate gains in productivity and lower
maintenance costs. Python runs on Windows, Linux/Unix, Mac OS X, and
has been ported to the Java and .NET virtual machines. Python is
free to use, even for commercial products, because of its OSI-
approved open source license. New to Python or choosing between
Python 2 and Python 3? Read Python 2 or Python 3. The Python
Software Foundation holds the intellectual property rights behind
Python, underwrites the PyCon conference, and funds many other
projects in the Python community.
'''
DATA = TXT.split()
DATA = DATA * 1000
time_function('listcompr', DATA)
time_function("mapfce", DATA)
time_function("forloop", DATA)
'''
@author: Jiri Vrany
'''
from math import log
def listcompr(oldlist):
'''
list comprehension
'''
return [log(s) for s in oldlist if s * 5 % 7 == 0]
def mapfilter(oldlist):
'''
built in map
'''
obj = map(log, filter(lambda x: x * 5 % 7 == 0, oldlist))
return list(obj)
def forloop(oldlist):
'''
built in map
'''
newlist = []
for c in oldlist:
if c * 5 % 7 == 0:
newlist.append(log(c))
return newlist
def time_function(fname, param, rep=10):
print (fname)
call_name = "{}({})".format(fname, param)
setup_name = "from __main__ import {}".format(fname)
print(timeit.timeit(call_name, setup=setup_name, number=rep))
def test_equality():
data = range(100)
assert forloop(data) == mapfilter(data)
assert mapfilter(data) == listcompr(data)
if __name__ == '__main__':
import timeit
DATA = range(1, 1000000)
time_function('listcompr', DATA)
time_function("mapfilter", DATA)
time_function("forloop", DATA)
'''
@author: Jiri Vrany
'''
def generatorem(oldlist):
'''
list generator
'''
iterator = (s.upper() for s in oldlist)
return list(iterator)
def listcompr(oldlist):
'''
list comprehension
'''
return [s.upper() for s in oldlist]
def mapfce(oldlist):
'''
built in map
'''
return map(str.upper, oldlist)
def forloop(oldlist):
'''
built in map
'''
newlist = []
for word in oldlist:
newlist.append(word.upper())
return newlist
def time_function(fname, param, rep=10):
print (fname)
call_name = "{}({})".format(fname, param)
setup_name = "from __main__ import {}".format(fname)
print(timeit.timeit(call_name, setup=setup_name, number=rep))
if __name__ == '__main__':
import timeit
TXT = '''Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.
Python runs on Windows, Linux/Unix, Mac OS X, and has been ported to the Java and .NET virtual machines.
Python is free to use, even for commercial products, because of its OSI-approved open source license.
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
The Python Software Foundation holds the intellectual property rights behind Python,
underwrites the PyCon conference, and funds many other projects in the Python
community.'''
DATA = TXT.split()
DATA = DATA*10000
time_function('generatorem', DATA)
time_function('listcompr', DATA)
time_function("mapfce", DATA)
time_function("forloop", DATA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment