版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
空间
- sys.getsizeof
from sys import getsizeof
from numpy import array
from pandas import DataFrame
r = range(10)
sg = ''.join(str(i) for i in r)
ls = list(r)
st = set(r)
dt = {i: i for i in r}
t = tuple(r)
a = array(ls)
df = DataFrame(ls)
class C:pass
def f():pass
for i in ['r', 'sg', 'ls', 'st', 'dt', 't', 'a', 'df', 'C', 'f']:
size = getsizeof(eval(i))
print('\n%2s' % i, '\033[0;7m \033[0m' * (size // 14), size)
# print(eval(i))
集合
空间占用大于字典
元组
空间占用小于列表
时间
Timer
from time import time
class T:
def __init__(self):
self.t = time()
def __del__(self):
t = time() - self.t
print('\n', '\033[0;7m \033[0m' * int(t * 20), t)
for
from numpy import array
r = range(500000)
sg = ''.join(str(i) for i in r)
ls = list(r)
dt = {i: i for i in r}
st = {i for i in r}
tu = tuple(r)
a = array(r)
for j in ['r', 'sg', 'ls', 'dt', 'st', 'tu', 'a', 'a.tolist()']:
t = T()
for i in eval(j):pass
del t
结果 | 时间 |
---|---|
生成器 | 0.09400558471679688 |
字符串 | 0.38802218437194824 |
列表 | 0.06100344657897949 |
字典 | 0.07000398635864258 |
集合 | 0.06800389289855957 |
元组 | 0.06100344657897949 |
numpy.array | 0.1370079517364502 |
numpy.array.tolist() | 0.128007173538208 |
元组
≈列表
>字典
≈集合
>生成器
>>numpy.array.tolist()
>numpy.array
>>>字符串
in
r = range(10000)
sg = ''.join(str(i) for i in r)
ls = [str(i) for i in r]
dt = {str(i): i for i in r}
st = {str(i) for i in r}
t1 = tuple(r)
t2 = tuple(ls)
t = T()
for i in ls:
if i in sg:pass # 字符串
t = T()
for i in ls:
if i in ls:pass # 列表
t = T()
for i in ls:
if i in dt:pass # 字典
t = T()
for i in ls:
if i in st:pass # 集合
t = T()
for i in ls:
if i in t1:pass # 元组(int)
t = T()
for i in ls:
if i in t2:pass # 元组
del t
结果 | 时间 |
---|---|
字符串 | 0.2870168685913086 |
列表 | 3.38619327545166 |
字典 | 0.005000591278076172 |
集合 | 0.004000186920166016 |
元组1 | 7.896451711654663 |
元组2 | 3.6772103309631348 |
集合
>字典
>>字符串
>>列表
>元组
[]
r = range(100000)
t = T()
ls1 = [i+1 for i in r]
ls2 = [i+2 for i in r]
t = T()
ls1, ls2 = [], []
for i in r:
ls1.append(i+1)
ls2.append(i+2)
del t
文件
from sys import getsizeof
from time import time
import os, pickle, json
class T:
def __init__(self):
self.t = time()
def __del__(self):
t = time() - self.t
print('\n', '\033[0;7m \033[0m' * int(t * 40), t)
b = {i: i for i in range(300000)}
txt = 'a.txt'
py = 'a.py'
js = 'a.json'
pkl = 'a.pickle'
with open(txt, 'w', encoding='utf-8') as f:
f.write(repr(b))
with open(py, 'w', encoding='utf-8') as f:
f.write('b=' + repr(b))
with open(js, 'w', encoding='utf-8') as f:
json.dump(b, f)
with open(pkl, 'wb') as f:
pickle.dump(b, f)
print(getsizeof(b))
for f in [txt, py, js, pkl]:
print(os.path.getsize(f))
t = T()
with open(txt, encoding='utf-8') as f:
b = f.read()
t = T()
from a import b
t = T()
with open(js, encoding='utf-8') as f:
b = json.load(f)
t = T()
with open(pkl, 'rb') as f:
b = pickle.load(f)
del t
pickle占用空间最小,读取速度较快,力荐~~
from XX
import xx
速度较慢