[英]Why does python implementation use 9 times more memory than C?
我编写了一个程序,在 python 和 C 中列出从 2 到用户给定数字的素数列表。我运行这两个程序,寻找相同数字的素数,并在活动监视器中查看它们各自的进程。 我发现 python 实现使用的内存正好是 C 实现的 9 倍。 为什么python需要这么多内存以及为什么要存储相同的整数数组的特定倍数? 以下是该程序的两种实现:
蟒蛇版本:
import math
import sys
top = int(input('Please enter the highest number you would like to have checked: '))
num = 3
prime_list = [2]
while num <= top:
n = 0
prime = True
while int(prime_list[n]) <= math.sqrt(num):
if num % prime_list[n] == 0:
prime = False
n = 0
break
n = n + 1
if prime == True:
prime_list.append(num)
prime = False
num = num + 1
print("I found ", len(prime_list), " primes")
print("The largest prime I found was ", prime_list[-1])
C版:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
int N;
int arraySize = 1;
int *primes = malloc(100*sizeof(int));
int isPrime = 1;
primes[0] = 2;
int timesRealloc = 0;
int availableSlots = 100;
printf("Please enter the largest number you want checked: \n");
scanf("%d", &N);
int j = 0;
int i;
for (i = 3; i <= N; i+=2){
j = 0;
isPrime = 1;
while (primes[j] <= sqrt(i)) {
if (i%primes[j] == 0) {
isPrime = 0;
break;
}
j++;
}
if (isPrime == 1){
primes[arraySize] = i;
arraySize++;
}
if (availableSlots == arraySize){
timesRealloc++;
availableSlots += 100;
primes = realloc(primes, availableSlots*sizeof(int));
}
}
printf("I found %d primes\n", arraySize);
printf("Memory was reallocated %d times\n", timesRealloc);
printf("The largest prime I found was %d\n", primes[(arraySize-1)]);
return 0;
}
>>> import sys
>>> sys.getsizeof(123456)
28
这是 C int
大小的7倍。 在 Python 3 中,整数是struct _longobject
又名PyLong
实例:
struct _longobject {
PyVarObject ob_base;
digit ob_digit[1];
};
PyVarObject
在哪里
typedef struct {
PyObject ob_base;
Py_ssize_t ob_size;
} PyVarObject;
和PyObject
是
typedef struct _object {
Py_ssize_t ob_refcnt;
struct _typeobject *ob_type;
} PyObject;
从中我们得到了该对象 123456 在 64 位 Python 构建中的以下内存使用情况:
Py_ssize_t
)&PyLong_Type
(类型&PyLong_Type
PyTypeObject *
Py_ssize_t
) 由于 123456 适合前 30 位,因此总和为 28,或7 * sizeof (int)
除了 Python list
中的每个元素都是PyObject *
指向实际对象的事实之外; 在 64 位 Python 构建中,这些指针中的每一个都是 64 位; 这意味着每个列表元素引用单独消耗的内存是 C int
两倍。
将 7 和 2 相加得到9 。
对于更高效的代码,您可以使用数组; 使用类型代码'i'
,内存消耗应该与 C 版本非常接近。 array
s 有append
方法,这要归功于它的增长数组应该比在 C / 中更容易使用realloc
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.