サブリストの長さの複雑な式を取り出して、素数スレッドのチャンピオン ソリューションをさらに最適化しようとしています。同じサブシーケンスの len() は遅すぎます。これは、len が高価で、サブシーケンスの生成にコストがかかるためです。これは関数を少し高速化するように見えますが、条件ステートメント内でのみ除算を行っているにもかかわらず、まだ除算を取り除くことができませんでした。もちろん、n*n の代わりに n のマーキングを開始する最適化を取り除くことで、長さの計算を単純化することもできます...
除算 / を整数除算 // に置き換えて、Python 3 または
from __future__ import division
また、この再帰式が numpy ソリューションの高速化に役立つ場合は興味深いですが、numpy をあまり使用した経験がありません。
コードに対して psyco を有効にすると、話はまったく異なりますが、アトキンスふるいコードは、この特別なスライス手法よりも高速になります。
import cProfile
def rwh_primes1(n):
# http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
""" Returns a list of primes < n """
sieve = [True] * (n//2)
for i in xrange(3,int(n**0.5)+1,2):
if sieve[i//2]:
sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]
def primes(n):
# http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
# recurrence formula for length by amount1 and amount2 Tony Veijalainen 2010
""" Returns a list of primes < n """
sieve = [True] * (n//2)
amount1 = n-10
amount2 = 6
for i in xrange(3,int(n**0.5)+1,2):
if sieve[i//2]:
## can you make recurrence formula for whole reciprocal?
sieve[i*i//2::i] = [False] * (amount1//amount2+1)
amount1-=4*i+4
amount2+=4
return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]
numprimes=1000000
print('Profiling')
cProfile.Profile.bias = 4e-6
for test in (rwh_primes1, primes):
cProfile.run("test(numprimes)")
プロファイリング (バージョン間の違いはあまりありません)
3 function calls in 0.191 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.006 0.006 0.191 0.191 <string>:1(<module>)
1 0.185 0.185 0.185 0.185 myprimes.py:3(rwh_primes1)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
3 function calls in 0.192 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.006 0.006 0.192 0.192 <string>:1(<module>)
1 0.186 0.186 0.186 0.186 myprimes.py:12(primes)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
興味深いことに、制限を 10**8 に増やし、プロファイリングを削除する関数にタイミング デコレータを配置します。
rwh_primes1 took 23.670 s
primes took 22.792 s
primesieve took 10.850 s
興味深いことに、素数のリストを生成せずにふるい自体を返す場合、時間は数リスト バージョンの約半分になります。