鱼C论坛

 找回密码
 立即注册
查看: 2781|回复: 6

[技术交流] C 和 Python简易性能对比

[复制链接]
发表于 2021-10-15 00:50:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

  1. #include <stdio.h>
  2. #include <time.h>

  3. int main() {
  4.     int num;
  5.     _Bool is_prime;
  6.     int start, end, count = 0;

  7.     start = time(NULL);

  8.     printf("Prime evaluation system ###\n");
  9.    
  10.     printf("Please enter a legal whole number: ");
  11.     scanf("%d", &num);

  12.     printf("The prime between 0 and %d are: ", num);

  13.     for(int i=2; i<=num; i++) {
  14.         is_prime = 1;
  15.         for(int j=2; j<i; j++) {
  16.             
  17.             if(!(i % j)) {
  18.                 is_prime = 0;
  19.                 break;
  20.             }

  21.         }
  22.         if(is_prime == 1) {
  23.             count ++;
  24.             printf("%d, ", i);
  25.         }
  26.     }

  27.     printf("\b\b \n");

  28.     printf("Version 2.0: The evaluating took %ds to finish and there\'re %d prime there.\n", (end = time(NULL)) - start, count);

  29.     return 0;
  30. }
复制代码


C version 2


  1. #include <stdio.h>
  2. #include <time.h>

  3. int main() {
  4.     int num;
  5.     _Bool is_prime;
  6.     int start, end, count = 0;

  7.     start = time(NULL);

  8.     printf("Prime evaluation system ###\n");
  9.    
  10.     printf("Please enter a legal whole number: ");
  11.     scanf("%d", &num);

  12.     printf("The prime between 0 and %d are: ", num);

  13.     for(int i=1; i<=num; i++) {
  14.         is_prime = 1;
  15.         for(int j=2; j<i/2; j++) {
  16.             
  17.             if(!(i % j)) {
  18.                 is_prime = 0;
  19.                 break;
  20.             }

  21.         }
  22.         if(is_prime == 1) {
  23.             count ++;
  24.             printf("%d, ", i);
  25.         }
  26.     }

  27.     printf("\b\b \n");

  28.     printf("The evaluating took %ds to finish and there\'re %d prime there.\n", (end = time(NULL)) - start, count);

  29.     return 0;
  30. }
复制代码


C version 3

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <math.h>

  4. int main() {
  5.     int num;
  6.     _Bool is_prime;
  7.     int start, end, count = 0;

  8.     start = time(NULL);

  9.     printf("Prime evaluation system ###\n");
  10.    
  11.     printf("Please enter a legal whole number: ");
  12.     scanf("%d", &num);

  13.     printf("The prime between 0 and %d are: ", num);

  14.     for(int i=1; i<=num; i++) {
  15.         is_prime = 1;
  16.         for(int j=2; j<sqrt(i); j++) {
  17.             
  18.             if(!(i % j)) {
  19.                 is_prime = 0;
  20.                 break;
  21.             }

  22.         }
  23.         if(is_prime == 1) {
  24.             count ++;
  25.             printf("%d, ", i);
  26.         }
  27.     }

  28.     printf("\b\b \n");

  29.     printf("The evaluating took %ds to finish and there\'re %d prime there.\n", (end = time(NULL)) - start, count);

  30.     return 0;
  31. }
复制代码



Python version 1:

  1. import time


  2. start = time.time()

  3. print("Prime number evaluation system ###")
  4. num = int(input("Please enter a legal numbers: "))
  5. count = 0

  6. print("The prime numbers between 0 and %d are: " % num, end="")

  7. for i in range(2, num + 1):
  8.     is_prime = True
  9.     for j in range(2, i):
  10.         if not i % j:
  11.             is_prime = False
  12.             break
  13.    
  14.     if is_prime == True:
  15.         count += 1
  16.         print("%d, " % i, end="")

  17. print("\b\b ")

  18. print("Version 1.0: The prime numbers evaluation tooks %ds to finish and there\'re %d primes there" % ((time.time() - start), count))
复制代码


Python version 2

  1. import time


  2. start = time.time()

  3. print("Prime number evaluation system ###")
  4. num = int(input("Please enter a legal numbers: "))
  5. count = 0

  6. print("The prime numbers between 0 and %d are: " % num, end="")

  7. for i in range(2, num + 1):
  8.     is_prime = True
  9.     for j in range(2, i // 2):
  10.         if not i % j:
  11.             is_prime = False
  12.             break
  13.    
  14.     if is_prime == True:
  15.         count += 1
  16.         print("%d, " % i, end="")

  17. print("\b\b ")

  18. print("Version 2.0: The prime numbers evaluation tooks %ds to finish and there\'re %d primes there" % ((time.time() - start), count))
复制代码


Python version 3

  1. import time
  2. import math


  3. start = time.time()

  4. print("Prime number evaluation system ###")
  5. num = int(input("Please enter a legal numbers: "))
  6. count = 0

  7. print("The prime numbers between 0 and %d are: " % num, end="")

  8. for i in range(2, num + 1):
  9.     is_prime = True
  10.     for j in range(2, int(math.sqrt(i))):
  11.         if not i % j:
  12.             is_prime = False
  13.             break
  14.    
  15.     if is_prime == True:
  16.         count += 1
  17.         print("%d, " % i, end="")

  18. print("\b\b ")

  19. print("Version 2.0: The prime numbers evaluation tooks %ds to finish and there\'re %d primes there" % ((time.time() - start), count))
复制代码


当然啦, 语言得看应用场景, 脱离场景谈性能,有时无疑是追求大炮大小鸟---费劲
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-14 05:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表