简体   繁体   English

循环运行所花费的时间

[英]Time taken for a loop to run

I am new to programming. 我是编程新手。 I was trying a program to print the nth prime number. 我正在尝试打印第n个质数的程序。 The code is as given below: 代码如下:

##program to print the nth prime number
nreq=input("Enter a number ")  
pctr=0 ##counter of the prime numbers
num=2

while pctr!=nreq:
    ctr=0 ##counter
    i=2
    while i<=num:
        if num%i==0:
            ctr=ctr+1
        i=i+1
    if ctr==1:
        pctr=pctr+1
    if pctr==nreq:
        break
    num=num+1


print 'the {}th prime number is {}'.format(nreq,num)  

I tried the same algorithm in both python and C. For larger numbers the time taken in python was much greater for the output as compared to C. Why did this occur? 我在python和C中都尝试了相同的算法。对于较大的数字,与C相比,python在输出中花费的时间要长得多。为什么会发生这种情况? Can someone please explain the difference in which both of them execute? 有人可以解释一下两者执行的区别吗?

My code in C was the following: 我在C中的代码如下:

#include<stdio.h>


void main()
{

int num=2,nreq,ctr=0,pctr=0,i;

printf("enter the number of prime required");
scanf("%d",&nreq);

    while(pctr!=nreq)
{

    ctr=0;
    for(i=2;i<=num;i++)
        if(num%i==0)
            ctr++;

    if(ctr==1)
        pctr++;

    if(pctr==nreq) 
        break;
    num++;
}

printf("the %dth prime number is %d",nreq,num);
}

C is often faster than Python as it is compiled, and is less abstract. C语言在编译时通常比Python更快 ,并且抽象度较低。 What that means is that C generates machine code, which is then executed by the computer. 这意味着C会生成机器代码,然后由计算机执行。 On the other hand, Python's interpreter is actually a C programs that goes through the syntax tree at runtime and executes it, which in many cases is slower. 另一方面,Python的解释器实际上是一个C程序,它在运行时遍历语法树并执行它,这在许多情况下会比较慢。 If you want your Python code to run faster, then you can try another Python implementation . 如果您希望Python代码运行得更快,则可以尝试其他Python实现

That being said, your algorithm is rather inefficient, O(n^2) (although I'm not sure if there is any less complex algorithm for this problem). 话虽这么说,您的算法效率很低,为O(n^2) (尽管我不确定是否有任何较简单的算法可以解决此问题)。 In other words, as the input gets larger, the worst case complexity scales much faster. 换句话说,随着输入的增加,最坏情况下的复杂性扩展得更快。 For more information on complexity, see here . 有关复杂性的更多信息,请参见此处

Edit: Thanks to @laindir for providing an explanation of how to make this code less complex: 编辑:感谢@laindir提供了有关如何使此代码不太复杂的解释:

An easy optimization is to store the primes so far discovered, so that only they are checked in the inner loop. 一种简单的优化方法是存储迄今发现的素数,以便仅在内部循环中对其进行检查。 That brings it down from O(n^2) to O(n * p) where p is the number of primes less than n. 这使它从O(n ^ 2)降到O(n * p),其中p是小于n的素数。 The next simplest is doing something in the outer loop to skip over numbers guaranteed not to be prime--a technique known as sieving--eg, skipping even numbers. 下一个最简单的方法是在外循环中执行某些操作以跳过保证不是质数的数字(一种称为筛选的技术),例如,跳过偶数。 A very simple sieve is Eratosthenes--that's the one used by the unix utility primes. 一种非常简单的筛子是Eratosthenes-这是unix实用程序质数使用的筛子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM