Created
April 21, 2012 19:30
-
-
Save ajdavis/2439224 to your computer and use it in GitHub Desktop.
Comparing mem usage of str catenation and list joining
This file contains 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 os, random, string | |
nobjs = int(1e7) | |
objsz = 1 | |
print 'str catenation' | |
s = '' | |
for i in range(nobjs): | |
randchars = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(objsz)) | |
s += randchars | |
os.system('ps -p %s -o "%%mem"' % os.getpid()) |
This file contains 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 os, random, string | |
nobjs = int(1e7) | |
objsz = 1 | |
print 'chunks' | |
chunks = [] | |
for i in range(nobjs): | |
randchars = ''.join(random.choice(string.ascii_letters) for x in range(objsz)) | |
chunks.append(randchars) | |
os.system('ps -p %s -o "%%mem"' % os.getpid()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ time python catenation.py
str catenation
%MEM
4.0
real 0m43.178s
user 0m42.979s
sys 0m0.191s
$ time chunks.py
chunks
%MEM
4.8
real 0m39.946s
user 0m39.707s
sys 0m0.231s