马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
C 和 Python的性能差别真TMD大,以求百万以内的素数为例, 算法越低效效果越明显:
运行环境: 相同
各版本的算法:
version 1: 逐个求余, 最低效
version 2: 前一半求余, 次之
version 3: 平方根求余, 最高效
结果:
version : lang: time: count
1: c : 190s : 78499
2: c: 98s: 78500
3: c : 4s : 78667
1: python: 不试了,时间太长,等不了
2: python: 2190s: 78500
3: python: 10s: 78704
上代码:
C version1#include <stdio.h>
#include <time.h>
int main() {
int num;
_Bool is_prime;
int start, end, count = 0;
start = time(NULL);
printf("Prime evaluation system ###\n");
printf("Please enter a legal whole number: ");
scanf("%d", &num);
printf("The prime between 0 and %d are: ", num);
for(int i=2; i<=num; i++) {
is_prime = 1;
for(int j=2; j<i; j++) {
if(!(i % j)) {
is_prime = 0;
break;
}
}
if(is_prime == 1) {
count ++;
printf("%d, ", i);
}
}
printf("\b\b \n");
printf("Version 2.0: The evaluating took %ds to finish and there\'re %d prime there.\n", (end = time(NULL)) - start, count);
return 0;
}
C version 2
#include <stdio.h>
#include <time.h>
int main() {
int num;
_Bool is_prime;
int start, end, count = 0;
start = time(NULL);
printf("Prime evaluation system ###\n");
printf("Please enter a legal whole number: ");
scanf("%d", &num);
printf("The prime between 0 and %d are: ", num);
for(int i=1; i<=num; i++) {
is_prime = 1;
for(int j=2; j<i/2; j++) {
if(!(i % j)) {
is_prime = 0;
break;
}
}
if(is_prime == 1) {
count ++;
printf("%d, ", i);
}
}
printf("\b\b \n");
printf("The evaluating took %ds to finish and there\'re %d prime there.\n", (end = time(NULL)) - start, count);
return 0;
}
C version 3#include <stdio.h>
#include <time.h>
#include <math.h>
int main() {
int num;
_Bool is_prime;
int start, end, count = 0;
start = time(NULL);
printf("Prime evaluation system ###\n");
printf("Please enter a legal whole number: ");
scanf("%d", &num);
printf("The prime between 0 and %d are: ", num);
for(int i=1; i<=num; i++) {
is_prime = 1;
for(int j=2; j<sqrt(i); j++) {
if(!(i % j)) {
is_prime = 0;
break;
}
}
if(is_prime == 1) {
count ++;
printf("%d, ", i);
}
}
printf("\b\b \n");
printf("The evaluating took %ds to finish and there\'re %d prime there.\n", (end = time(NULL)) - start, count);
return 0;
}
Python version 1:import time
start = time.time()
print("Prime number evaluation system ###")
num = int(input("Please enter a legal numbers: "))
count = 0
print("The prime numbers between 0 and %d are: " % num, end="")
for i in range(2, num + 1):
is_prime = True
for j in range(2, i):
if not i % j:
is_prime = False
break
if is_prime == True:
count += 1
print("%d, " % i, end="")
print("\b\b ")
print("Version 1.0: The prime numbers evaluation tooks %ds to finish and there\'re %d primes there" % ((time.time() - start), count))
Python version 2import time
start = time.time()
print("Prime number evaluation system ###")
num = int(input("Please enter a legal numbers: "))
count = 0
print("The prime numbers between 0 and %d are: " % num, end="")
for i in range(2, num + 1):
is_prime = True
for j in range(2, i // 2):
if not i % j:
is_prime = False
break
if is_prime == True:
count += 1
print("%d, " % i, end="")
print("\b\b ")
print("Version 2.0: The prime numbers evaluation tooks %ds to finish and there\'re %d primes there" % ((time.time() - start), count))
Python version 3import time
import math
start = time.time()
print("Prime number evaluation system ###")
num = int(input("Please enter a legal numbers: "))
count = 0
print("The prime numbers between 0 and %d are: " % num, end="")
for i in range(2, num + 1):
is_prime = True
for j in range(2, int(math.sqrt(i))):
if not i % j:
is_prime = False
break
if is_prime == True:
count += 1
print("%d, " % i, end="")
print("\b\b ")
print("Version 2.0: The prime numbers evaluation tooks %ds to finish and there\'re %d primes there" % ((time.time() - start), count))
当然啦, 语言得看应用场景, 脱离场景谈性能,有时无疑是追求大炮大小鸟---费劲 |