Re: What is the "functional" way of doing this?
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 01 Aug 2007 02:57:02 +1000
On Tue, 31 Jul 2007 09:01:42 -0300, Ricardo Aráoz wrote:
Considering I am a beginner I did a little test. Funny results too. The
function I proposed (lists1.py) took 11.4529998302 seconds, while the
other one (lists2.py) took 16.1410000324 seconds, thats about 40% more.
They were run in IDLE from their own windows (F5).
[snip code]
You may find that using the timeit module is better than rolling your own
timer.
.... if n > 0:def recursive_func(n):
.... return [n % 26] + recursive_func(n/26)
.... else:
.... return []
....
.... def mseq(n):def generator_func(n):
.... while n > 0:
.... n, a = divmod(n, 26)
.... yield a
.... return list(mseq(n))
....
.... "from __main__ import N, recursive_func").repeat()
import timeit
N = 10**6+1
timeit.Timer("recursive_func(N)",
[16.48972487449646, 17.000514984130859, 16.520529985427856]
.... "from __main__ import N, generator_func").repeat()
timeit.Timer("generator_func(N)",
[27.938560009002686, 28.970781087875366, 23.977837085723877]
If you're going to compare speeds, you should also test this one:
.... results = []def procedural_func(n):
.... while n > 0:
.... n, a = divmod(n, 26)
.... results.append(a)
.... return results
....
.... "from __main__ import N, procedural_func").repeat()
timeit.Timer("procedural_func(N)",
[15.577107906341553, 15.60145378112793, 15.345284938812256]
I must admit that I'm surprised at how well the recursive version did, and
how slow the generator-based version was. But I'd be careful about drawing
grand conclusions about the general speed of recursion etc. in Python from
this one single example. I think this is simply because the examples tried
make so few recursive calls. Consider instead an example that makes a few
more calls:
.... "from __main__ import N, recursive_func").repeat(3, 10000)N = 26**100 + 1
timeit.Timer("recursive_func(N)",
[7.0015969276428223, 7.6065640449523926, 6.8495190143585205]
.... "from __main__ import N, generator_func").repeat(3, 10000)timeit.Timer("generator_func(N)",
[3.56563401222229, 3.1132731437683105, 3.8274538516998291]
.... "from __main__ import N, procedural_func").repeat(3, 10000)timeit.Timer("procedural_func(N)",
[3.3509068489074707, 4.0872640609741211, 3.3742849826812744]
--
Steven.
.
- Follow-Ups:
- Re: What is the "functional" way of doing this?
- From: Ricardo Aráoz
- Re: What is the "functional" way of doing this?
- References:
- What is the "functional" way of doing this?
- From: beginner
- Re: What is the "functional" way of doing this?
- From: attn.steven.kuo@xxxxxxxxx
- Re: What is the "functional" way of doing this?
- From: Paul Rubin
- Re: What is the "functional" way of doing this?
- From: attn.steven.kuo@xxxxxxxxx
- Re: What is the "functional" way of doing this?
- From: Ricardo Aráoz
- What is the "functional" way of doing this?
- Prev by Date: encode() question
- Next by Date: Re: encode() question
- Previous by thread: Re: What is the "functional" way of doing this?
- Next by thread: Re: What is the "functional" way of doing this?
- Index(es):
Relevant Pages
|