Path: csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!news2.euro.net!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.02; 'example:': 0.03; 'builtin': 0.07; 'function,': 0.07; 'pypy': 0.07; 'python': 0.09; 'str,': 0.09; 'subject:into': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'lambda': 0.16; 'lot!': 0.16; 'map(int,': 0.16; 'negates': 0.16; 'sequence:': 0.16; 'subject:Converting': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.17; 'jan': 0.18; 'code.': 0.20; 'int,': 0.22; 'runs': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'besides': 0.27; 'subject:list': 0.28; "d'aprano": 0.29; 'overhead': 0.29; 'steven': 0.29; 'function': 0.30; '(and': 0.32; 'int': 0.33; 'picking': 0.33; 'thanks': 0.34; 'faster': 0.35; 'especially': 0.35; 'subject:?': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'your': 0.60; 'map': 0.61; 'note:': 0.64; 'results': 0.65; 'header:Reply-To:1': 0.68; 'applying': 0.69; 'reply-to:no real name:2**0': 0.72; 'you:': 0.75; 'gain': 0.79; 'clarifies': 0.84 Date: Sun, 22 Jul 2012 20:27:29 +0300 From: Jan Riechers User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: Steven D'Aprano Subject: Re: Converting a list of strings into a list of integers? References: <3rCdnUCiWpP1gZHNnZ2dnUVZ7vQAAAAA@giganews.com> <500c31d7$0$29978$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <500c31d7$0$29978$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: janpeterr@freenet.de 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342978199 news.xs4all.nl 6892 [2001:888:2000:d::a6]:59437 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25827 On 22.07.2012 20:01, Steven D'Aprano wrote: [SNIP] > map is faster than an ordinary for-loop if the function you are applying > is a builtin like int, str, etc. But if you have to write your own pure- > Python function, the overhead of calling a function negates the advantage > of map, which is no faster than a for-loop. For example: > > results = map(int, sequence) # calls builtin `int` > > hoists the call to int into the fast C layer, instead of the slow Python > layer, and should be faster than > > results = [] > for x in sequence: > results.append(int(x)) > > which runs at the speed of Python. But: > > results = map(lambda x: x+1, sequence) # calls pure Python function > > if no faster than a for-loop: > > results = [] > for x in sequence: > results.append(x+1) > > Note: this has*nothing* to do with the use of lambda. Writing the "+1" > function above using def instead of lambda would give the same results. [SNAP] Hi Steven, besides that I testdrive Pypy (and still am impressed, other topic) - your answer was what I was picking for ;) Especially this part of you: > map is faster than an ordinary for-loop if the function you are applying > is a builtin like int, str, etc. [underlaying c-layer] But if you have to write your own pure- > Python function, the overhead of calling a function negates the advantage > of map [...] I did not know that the speed gain is up foremost present when using built-ins, but that's for sure something to keep in mind when writing code. Thanks for your explanation, clarifies a lot! Jan