Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25827

Re: Converting a list of strings into a list of integers?

Date 2012-07-22 20:27 +0300
From Jan Riechers <janpeterr@freenet.de>
Subject Re: Converting a list of strings into a list of integers?
References <3rCdnUCiWpP1gZHNnZ2dnUVZ7vQAAAAA@giganews.com> <e9VOr.124006$PE.28478@fx04.am4> <mailman.2430.1342974153.4697.python-list@python.org> <500c31d7$0$29978$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.2436.1342978199.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 10:29 -0500
  Re: Converting a list of strings into a list of integers? Roy Smith <roy@panix.com> - 2012-07-22 11:39 -0400
    Re: Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 11:01 -0500
      Re: Converting a list of strings into a list of integers? Peter Otten <__peter__@web.de> - 2012-07-22 18:30 +0200
  Re: Converting a list of strings into a list of integers? Alister <alister.ware@ntlworld.com> - 2012-07-22 15:39 +0000
    Re: Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 11:01 -0500
    Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 19:20 +0300
      Re: Converting a list of strings into a list of integers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-22 17:01 +0000
        Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 20:27 +0300
      Re: Converting a list of strings into a list of integers? Grant Edwards <invalid@invalid.invalid> - 2012-07-23 14:27 +0000
        Re: Converting a list of strings into a list of integers? rusi <rustompmody@gmail.com> - 2012-07-23 08:31 -0700
    Re: Converting a list of strings into a list of integers? David Robinow <drobinow@gmail.com> - 2012-07-22 13:03 -0400
    Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 20:10 +0300
    Re: Converting a list of strings into a list of integers? Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-22 11:16 -0600
  Re: Converting a list of strings into a list of integers? Paul Rubin <no.email@nospam.invalid> - 2012-07-22 10:03 -0700
  Re: Converting a list of strings into a list of integers? Dave Angel <d@davea.name> - 2012-07-22 21:03 -0400

csiph-web