使用psyco和Eratosthenes筛子显着的结果 [英] Remarkable results with psyco and sieve of Eratosthenes

查看:97
本文介绍了使用psyco和Eratosthenes筛子显着的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想用psyco实验

时报告一个令人愉快的小惊喜。

下面的程序与psyco表现得非常好。


它找到所有素数< 10,000,000


处理器是AMD64 4000+运行32位。


非psyco''d python版需要94秒。


psyco''d版需要9.6秒。


但这里是踢球者。使用gcc -O3编译的C和

编写的算法相同,需要4.5秒。在这个测试中,Python的运行速度只有原来的一半。


让我的一天,我想分享我的发现。


顺便说一句,这段代码可以更高效吗?

============


#!/ usr / bin / python -OO

导入数学

导入系统

导入psyco


psyco.full()


def primes():

primes = [3]
$ x $ b for x in xrange( 5,10000000,2):

maxfact = int(math.sqrt(x))

flag = True

for primes:

如果y maxfact:

休息

如果x%y == 0:

flag = False

休息

if flag == True:

primes.append(x)

primes()

解决方案


#!/ usr / bin / python -OO

导入数学

导入系统

导入psyco


psyco.full()


def primes():

primes = [3]
$ x $ b for x in xrange(5,10000000,2):

maxfact = int(math.sqrt(x))

flag = True

for primes:

if y maxfact:

break

if x% y == 0:

flag = False

break

if flag == True:

primes.append (x)

primes()



一些微不足道的优化。给它一个旋转。


def primes():

sqrt = math.sqrt

primes = [3]

for x in xrange(5,10000000,2):

maxfact = int(sqrt(x))

为素数中的y:

如果y maxfact:

primes.append(x)

break

如果不是x%y:

休息

返回素数


-

博客: http://www.willmcgugan.com


Steve Bergman写道:


只是想用psyco实验

时报告一个令人愉快的小惊喜。

下面的程序表现得非常好psyco。


找到所有素数< 10,000,000



Actualy,它不是。你忘记了1和2.

将McGugan

-

博客: http://www.willmcgugan.com


Will McGugan写道:


史蒂夫伯格曼写道:


只是想用psyco实验

时报告一个令人愉快的小惊喜。

下面的程序与psyco表现得非常好。


它找到了所有素数< 10,000,000



Actualy,它不是。你忘记了1和2.



数字1通常不被认为是素数 - 请参阅
http://mathworld.wolfram.com/PrimeNumber.html


Just wanted to report a delightful little surprise while experimenting
with psyco.
The program below performs astonoshingly well with psyco.

It finds all the prime numbers < 10,000,000

Processor is AMD64 4000+ running 32 bit.

Non psyco''d python version takes 94 seconds.

psyco''d version takes 9.6 seconds.

But here is the kicker. The very same algorithm written up in C and
compiled with gcc -O3, takes 4.5 seconds. Python is runng half as fast
as optimized C in this test!

Made my day, and I wanted to share my discovery.

BTW, can this code be made any more efficient?
============

#!/usr/bin/python -OO
import math
import sys
import psyco

psyco.full()

def primes():
primes=[3]
for x in xrange(5,10000000,2):
maxfact = int(math.sqrt(x))
flag=True
for y in primes:
if y maxfact:
break
if x%y == 0:
flag=False
break
if flag == True:
primes.append(x)
primes()

解决方案

#!/usr/bin/python -OO
import math
import sys
import psyco

psyco.full()

def primes():
primes=[3]
for x in xrange(5,10000000,2):
maxfact = int(math.sqrt(x))
flag=True
for y in primes:
if y maxfact:
break
if x%y == 0:
flag=False
break
if flag == True:
primes.append(x)
primes()

Some trivial optimizations. Give this a whirl.

def primes():
sqrt=math.sqrt
primes=[3]
for x in xrange(5,10000000,2):
maxfact = int(sqrt(x))
for y in primes:
if y maxfact:
primes.append(x)
break
if not x%y:
break
return primes

--
blog: http://www.willmcgugan.com


Steve Bergman wrote:

Just wanted to report a delightful little surprise while experimenting
with psyco.
The program below performs astonoshingly well with psyco.

It finds all the prime numbers < 10,000,000

Actualy, it doesn''t. You forgot 1 and 2.
Will McGugan
--
blog: http://www.willmcgugan.com


Will McGugan wrote:

Steve Bergman wrote:

Just wanted to report a delightful little surprise while experimenting
with psyco.
The program below performs astonoshingly well with psyco.

It finds all the prime numbers < 10,000,000


Actualy, it doesn''t. You forgot 1 and 2.

The number 1 is not generally considered to be a prime number -- see
http://mathworld.wolfram.com/PrimeNumber.html .


这篇关于使用psyco和Eratosthenes筛子显着的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆