Created
November 6, 2010 15:03
-
-
Save zed/665475 to your computer and use it in GitHub Desktop.
time performance: string.join vs. string.__add__
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
| #!/usr/bin/env python | |
| from profilestats import profile # $ pip install profilestats | |
| def join_join(a, b): | |
| return ','.join([a, b]) | |
| def join_plus(a, b): | |
| return a + ',' + b | |
| @profile | |
| def test(): | |
| N = 10**4 | |
| for a, b in generate_str_pairs(N): | |
| assert join_plus(a, b) == join_join(a, b) | |
| for a, b in generate_str_pairs(N): | |
| assert join_join(a, b) == join_plus(a, b) | |
| def generate_str_pairs(n): | |
| for i in xrange(n): | |
| yield 'a'*i, 'b'*i | |
| if __name__ == "__main__": | |
| test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment