Skip to content

Instantly share code, notes, and snippets.

@ajdavis
Created April 21, 2012 19:30
Show Gist options
  • Save ajdavis/2439224 to your computer and use it in GitHub Desktop.
Save ajdavis/2439224 to your computer and use it in GitHub Desktop.
Comparing mem usage of str catenation and list joining
import os, random, string
nobjs = int(1e7)
objsz = 1
print 'str catenation'
print
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())
import os, random, string
nobjs = int(1e7)
objsz = 1
print 'chunks'
print
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())
@ajdavis
Copy link
Author

ajdavis commented Apr 21, 2012

$ 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment