Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我有两个Python代码用于查找素数。为什么在这两个代码中,一个生成的结果比另一个快得多_Python_Performance_Primes - Fatal编程技术网

我有两个Python代码用于查找素数。为什么在这两个代码中,一个生成的结果比另一个快得多

我有两个Python代码用于查找素数。为什么在这两个代码中,一个生成的结果比另一个快得多,python,performance,primes,Python,Performance,Primes,我已经编写了寻找素数的程序,使用尽可能多的技术来寻找素数。然而,当我在互联网上搜索时(在GeekforGeek上),我发现了一个类似于我的(算法思想相同),但产生相同结果的速度要快得多。我想知道这两者有什么不同 我们都减少了测试1)只检查奇数。2) 使除数仅位于奇数3)仅允许除数达到该数字的平方根 #my code import time import math start = time.time() upperlimit = 1000000 counter = 1 number = 3 wh

我已经编写了寻找素数的程序,使用尽可能多的技术来寻找素数。然而,当我在互联网上搜索时(在GeekforGeek上),我发现了一个类似于我的(算法思想相同),但产生相同结果的速度要快得多。我想知道这两者有什么不同

我们都减少了测试1)只检查奇数。2) 使除数仅位于奇数3)仅允许除数达到该数字的平方根

#my code
import time
import math
start = time.time()
upperlimit = 1000000 
counter = 1
number = 3
while number<upperlimit: #loop to check number
    shittyvalue = 0
    division = 3
    while math.sqrt(number) >= division: # conditional loop
        if number % division == 0:
            shittyvalue = 1 #for giving the annoucement on whether this number is a prime
            break
        division = division + 2
    if shittyvalue == 0:
        counter = counter + 1
    number = number + 2
print ("There are ",counter, " prime numbers")
end = time.time()
print ("Found in ",end-start, " seconds")
#我的代码
导入时间
输入数学
开始=时间。时间()
上限=1000000
计数器=1
数字=3
当数字=除法时:#条件循环
如果数字%division==0:
shittyvalue=1#用于说明该数字是否为素数
打破
除法=除法+2
如果shittyvalue==0:
计数器=计数器+1
数字=数字+2
打印(“有”,计数器,“素数”)
end=time.time()
打印(“在中找到”,结束-开始,“秒”)
#GeekforGeek代码
#Python程序,用于查找范围内的素数
输入数学
导入时间
def是_prime(n):
如果n2和n%2==0:
返回错误
max_div=数学地板(数学sqrt(n))
对于范围内的i(3,1+最大刻度,2):
如果n%i==0:
返回错误
返回真值
#驱动函数
t0=时间。时间()
c=0#用于计数
对于范围内的n(11000000):
x=是素数(n)
c+=x
打印(“范围内的总质数:”,c)
t1=时间。时间()
打印(“所需时间:”,t1-t0)
结果表明:

我的:有78498个素数 发现时间为17.29092025756836秒

GeekforGeek's:范围内的总质数:78498 所需时间:3.9572863578796387

您可以将Math.sqrt(数字)从while循环中去掉。当n较大时,这是一个繁重的操作

For循环比Python中的while循环快
有两个主要原因

1)
math.sqrt(number)
重复执行

2)
与相比:

import time

upper_limit = 1000000


def test_while_loop():
    start = time.time()

    sum = 0

    idx = 0
    while idx < upper_limit:
        sum += idx
        idx += 2

    end = time.time()

    print("Found in", end - start, " seconds")
    print("Sum", sum)


test_while_loop()


def test_for_loop():
    start = time.time()

    sum = 0

    for idx in range(0, upper_limit, 2):
        sum += idx

    end = time.time()

    print ("Found in", end - start, " seconds")
    print("Sum", sum)


test_for_loop()

一个明显的区别是:第一个版本在内部循环的每次迭代中计算一个平方根,第二个版本在每个被测试的数字中只计算一次。
import time

upper_limit = 1000000


def test_while_loop():
    start = time.time()

    sum = 0

    idx = 0
    while idx < upper_limit:
        sum += idx
        idx += 2

    end = time.time()

    print("Found in", end - start, " seconds")
    print("Sum", sum)


test_while_loop()


def test_for_loop():
    start = time.time()

    sum = 0

    for idx in range(0, upper_limit, 2):
        sum += idx

    end = time.time()

    print ("Found in", end - start, " seconds")
    print("Sum", sum)


test_for_loop()
Found in 0.0648810863494873  seconds
Sum 249999500000
Found in 0.02890491485595703  seconds
Sum 249999500000