Created
March 18, 2014 10:58
-
-
Save mozeq/9617870 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
def test1(response): | |
return response.split(' ', 1) if ' ' in response else (response, '') | |
def test2(response): | |
parts = response.split(' ', 1) | |
if len(parts) > 1: | |
return parts[0], parts[1] | |
else: | |
return response, '' | |
def test3(response): | |
try: | |
status, msg = response.split(' ', 1) | |
return status, msg | |
except ValueError: | |
return response, '' | |
print 'in', timeit.timeit('test1("success blabla blabla bla")', setup='from __main__ import test1') | |
print 'in2', timeit.timeit('test1("fail")', setup='from __main__ import test1') | |
print 'len', timeit.timeit('test2("success blabla blabla bla")', setup='from __main__ import test2') | |
print 'len2', timeit.timeit('test2("fail")', setup='from __main__ import test2') | |
print 'try', timeit.timeit('test3("success blabla blabla bla")', setup='from __main__ import test3') | |
print 'try2', timeit.timeit('test3("fail")', setup='from __main__ import test3') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment