Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'directions': 0.04; 'memory.': 0.05; 'subject:Python': 0.05; 'python': 0.08; '[1]:': 0.09; '[2]:': 0.09; 'function:': 0.09; 'garbage': 0.09; 'def': 0.13; '0.03': 0.16; '[4]:': 0.16; 'centos': 0.16; 'creation,': 0.16; 'cyclic': 0.16; 'did:': 0.16; 'explanation': 0.16; 'pythons': 0.16; 'row.': 0.16; 'subject:memory': 0.16; 'switching': 0.16; 'test()': 0.16; 'test():': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.20; 'memory': 0.21; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'sep': 0.23; 'cc:2**0': 0.24; 'sender:addr:gmail.com': 0.25; 'creating': 0.25; 'tests': 0.25; "i'm": 0.26; 'import': 0.27; 'url:edu': 0.27; 'produced': 0.28; 'message-id:@mail.gmail.com': 0.28; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'ran': 0.30; 'tuples': 0.30; 'thanks': 0.31; 'time:': 0.32; 'total:': 0.32; 'list': 0.32; 'objects': 0.32; 'tue,': 0.32; 'decide': 0.33; 'received:209.85.212': 0.34; '2.4': 0.34; 'see,': 0.34; 'test': 0.35; 'solutions.': 0.37; 'run': 0.37; 'machine': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'skip:\xa0 10': 0.39; 'received:209': 0.40; '8bit%:8': 0.40; 'more': 0.61; 'according': 0.61; 'your': 0.61; 'here': 0.65; '2012': 0.67; 'collection': 0.69; 'suited': 0.73; 'million': 0.76; '9.15': 0.84; 'experiencing': 0.84; 'heuristic': 0.84; 'otten': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=euzkHXWOSsREGR34p7yNSZzpg2463jfyezOx/N6hrgY=; b=f/f9kcM0Tsl3ElDZVkm8i35jzM3dUUeOu7GdEXKpm5b+SwlvrkLzHOZy5sv1r/Kiub fMZ/pTlr+px3DDgjf2zMYIsJRJbKycSUvATkp8IIAkRJe/B6ZSpf1cjUUlJTbG0uObyo 1dmzni4iML8mrhjvE6TpRMI8LXlP1cveYrsXo= MIME-Version: 1.0 Sender: benoit.thiell.cfa@gmail.com In-Reply-To: References: Date: Wed, 4 Jan 2012 09:57:56 -0500 X-Google-Sender-Auth: seiOpkhTyr9eIm9NjMeizKI6r34 Subject: Re: Large list in memory slows Python From: Benoit Thiell To: Peter Otten <__peter__@web.de> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325689078 news.xs4all.nl 6929 [2001:888:2000:d::a6]:36372 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18500 On Tue, Jan 3, 2012 at 5:59 PM, Peter Otten <__peter__@web.de> wrote: > Benoit Thiell wrote: > >> I am experiencing a puzzling problem with both Python 2.4 and Python >> 2.6 on CentOS 5. I'm looking for an explanation of the problem and >> possible solutions. Here is what I did: >> >> Python 2.4.3 (#1, Sep 21 2011, 19:55:41) >> IPython 0.8.4 -- An enhanced Interactive Python. >> >> In [1]: def test(): >> =A0 =A0...: =A0 =A0 return [(i,) for i in range(10**6)] >> >> In [2]: %time x =3D test() >> CPU times: user 0.82 s, sys: 0.04 s, total: 0.86 s >> Wall time: 0.86 s >> >> In [4]: big_list =3D range(50 * 10**6) >> >> In [5]: %time y =3D test() >> CPU times: user 9.11 s, sys: 0.03 s, total: 9.14 s >> Wall time: 9.15 s >> >> As you can see, after creating a list of 50 million integers, creating >> the same list of 1 million tuples takes about 10 times longer than the >> first time. >> >> I ran these tests on a machine with 144GB of memory and it is not >> swapping. Before creating the big list of integers, IPython used 111MB >> of memory; After the creation, it used 1664MB of memory. > > In older Pythons the heuristic used to decide when to run the cyclic garb= age > collection is not well suited for the creation of many objects in a row. > Try switching it off temporarily with > > import gc > gc.disable() > # create many objects that are here to stay > gc.enable() > > You may also encorporate that into your test function: > > def test(): > =A0 =A0gc.disable() > =A0 =A0try: > =A0 =A0 =A0 =A0return [...] > =A0 =A0finally: > =A0 =A0 =A0 =A0gc.enable() Thanks Peter, this is very helpful. Modifying my test according to your directions produced much more consistent results. Benoit. --=20 Benoit Thiell The SAO/NASA Astrophysics Data System http://adswww.harvard.edu/