Skip to content

Instantly share code, notes, and snippets.

@gthank
Created July 25, 2012 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gthank/3178199 to your computer and use it in GitHub Desktop.
Save gthank/3178199 to your computer and use it in GitHub Desktop.
list_comp vs. map
#! /usr/bin/env python
import timeit
def main():
list_comp_t = timeit.Timer("y = [math.log10(num) for num in x]", setup="import math; x = [1500, 1049.8, 34, 351]")
map_t = timeit.Timer("z = map(math.log10, x)", setup="import math; x = [1500, 1049.8, 34, 351]")
# Use an absurdly high number of reps because the data is tiny and I want the numbers to be on a reasonably human timescale.
print list_comp_t.timeit(1000000)
print map_t.timeit(1000000)
if __name__ == '__main__':
main()
main()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment