nbytes和getsizeof返回不同的值 [英] nbytes and getsizeof return different values

查看:101
本文介绍了nbytes和getsizeof返回不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当将库存入NumPy数组时,nbytesgetsizeof返回两个不同的值.

I have noticed that nbytes and getsizeof returns two different values when the bank to a NumPy array.

示例:

import sys
import numpy as np
x = np.random.rand(10000, 50)
print('x.nbytes: {0} bytes'.format(x.nbytes))
print('sys.getsizeof(x): {0} bytes'.format(sys.getsizeof(x)))

输出:

x.nbytes: 4000000 bytes
sys.getsizeof(x): 4000112 bytes

为什么?

推荐答案

这是因为nbytes仅返回存储数据的大小,而sys.getsizeof返回整个对象的大小,即存储的数据的大小加上一些开销. numpy数组的开销很小,因为它们必须存储数组尺寸,数据类型和类似信息.

This is because nbytes only returns the size of stored data, while sys.getsizeof returns the size of the whole object, i.e. the size of the stored data plus some overhead. There is a small overhead for numpy arrays, because they have to store the array dimensions, the datatype and similar information.

这篇关于nbytes和getsizeof返回不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆