Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed4a.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; 'explicitly': 0.05; 'indexing': 0.07; 'memory.': 0.07; 'string': 0.09; '(unicode': 0.09; '32-bit': 0.09; 'falls': 0.09; 'worse': 0.09; 'python': 0.11; '"python': 0.16; '-tkc': 0.16; 'do!': 0.16; 'encodings': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'opposite': 0.16; 'string)': 0.16; 'width.': 0.16; 'zero,': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'pointed': 0.19; "skip:' 30": 0.19; '>>>': 0.22; 'memory': 0.22; 'case.': 0.24; 'header:In-Reply- To:1': 0.27; "doesn't": 0.30; 'characters': 0.30; 'gives': 0.31; "skip:' 10": 0.31; 'subject:size': 0.31; '(e.g.': 0.33; 'cases': 0.33; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'space': 0.40; 'full': 0.61; 'back': 0.62; 'save': 0.62; 'email addr:gmail.com': 0.63; 'saving': 0.69; 'gain': 0.79; 'everything,': 0.84; 'received:50.22': 0.84 Date: Mon, 10 Feb 2014 08:43:08 -0600 From: Tim Chase To: python-list@python.org Subject: Re: Finding size of Variable In-Reply-To: References: <8e4c1ab1-e65d-483f-ad9d-6933ae2052c3@googlegroups.com> <7e7d3200-a4ae-4842-ad8d-68b4435b9006@googlegroups.com> <52f219c5$0$29972$c3e8da3$5496439d@news.astraweb.com> <888bd2fc-54b0-4c46-9d7b-d81d01a78b52@googlegroups.com> <52f59aeb$0$29972$c3e8da3$5496439d@news.astraweb.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392043356 news.xs4all.nl 2847 [2001:888:2000:d::a6]:36104 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65819 On 2014-02-10 06:07, wxjmfauth@gmail.com wrote: > Python does not save memory at all. A str (unicode string) > uses less memory only - and only - because and when one uses > explicitly characters which are consuming less memory. > > Not only the memory gain is zero, Python falls back to the > worse case. > > >>> sys.getsizeof('a' * 1000000) > 1000025 > >>> sys.getsizeof('a' * 1000000 + 'oe') > 2000040 > >>> sys.getsizeof('a' * 1000000 + 'oe' + '\U00010000') > 4000048 If Python used UTF-32 for EVERYTHING, then all three of those cases would be 4000048, so it clearly disproves your claim that "python does not save memory at all". > The opposite of what the utf8/utf16 do! > > >>> sys.getsizeof(('a' * 1000000 + 'oe' + > >>> '\U00010000').encode('utf-8')) > 1000023 > >>> sys.getsizeof(('a' * 1000000 + 'oe' + > >>> '\U00010000').encode('utf-16')) > 2000025 However, as pointed out repeatedly, string-indexing in fixed-width encodings are O(1) while indexing into variable-width encodings (e.g. UTF8/UTF16) are O(N). The FSR gives the benefits of O(1) indexing while saving space when a string doesn't need to use a full 32-bit width. -tkc