Skip to content

Instantly share code, notes, and snippets.

@davidwtbuxton
Last active December 14, 2017 18:50
Show Gist options
  • Save davidwtbuxton/9af0250ce66ccb64eee4214ab4ab070e to your computer and use it in GitHub Desktop.
Save davidwtbuxton/9af0250ce66ccb64eee4214ab4ab070e to your computer and use it in GitHub Desktop.
Profiling different ways for testing if there's an empty string value
$ python -m timeit -s "a, b, c, d, e, f = 'abcdef'" "any(v for v in (a, b, c, d, e, f) if v == '')"
1000000 loops, best of 3: 1.51 usec per loop
$ python -m timeit -s "a, b, c, d, e, f = 'abcdef'" "'' in (a, b, c, d, e, f)"
1000000 loops, best of 3: 0.36 usec per loop
$ python -m timeit -s "a, b, c, d, e, f = ' bcdef'" "any(v for v in (a, b, c, d, e, f) if v == '')"
1000000 loops, best of 3: 1.52 usec per loop
$ python -m timeit -s "a, b, c, d, e, f = ' bcdef'" "'' in (a, b, c, d, e, f)"
1000000 loops, best of 3: 0.354 usec per loop
$ python -m timeit -s "a, b, c, d, e, f = 'abcde '" "any(v for v in (a, b, c, d, e, f) if v == '')"
1000000 loops, best of 3: 1.49 usec per loop
$ python -m timeit -s "a, b, c, d, e, f = 'abcde '" "'' in (a, b, c, d, e, f)"
1000000 loops, best of 3: 0.357 usec per loop
$ python --version
Python 2.7.14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment