第十节-常用技术

id() 函数用于获取对象的内存地址
>>>a = 'runoob' 
>>> id(a) 
4531887632 
>>> b = 1 
>>> id(b) 
140588731085608
习题1: 练习序列化与反序列,
将一个字典对象序列化
复制一份文件,
再反序列化复制出来的文件
dumps、loads 是对字符序列,包括列表和字典的操作,
dump、load 是对文件的操作,序列化和反序列化
习题1: 序列化与反序列,
将一个字典对象序列化
复制一份文件,
再反序列化复制出来的文件
"""
#序列化
import json
dict1 = {'a':'1','b':'2','c':'3'}
with open("dict1.json","w") as file_in:
    json.dump(dict1,file_in,indent=4)
#反序列化
with open("dict1.json","r") as file_in:
    new_dict1 = json.load(file_in)
    print(new_dict1)


from timeit import timeit
#字符串的序列化和反序列化
print(timeit("""
import pickle
b = pickle.dumps(dict1)
print(b)
print(pickle.loads(b))
""",globals = globals()))

#用pickle dump load序列化、反序列化
import pickle
with open("./test_data/data.pickle","wb") as file_in:
    pickle.dump(dict1,file_in)
with open("./test_data/data.pickle","rb")as file_out:
    a = pickle.load(file_out)
    print("a =",a)
>>
{'a': '1', 'b': '2', 'c': '3'}
b'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01X\x01\x00\x00\x001q\x02X\x01\x00\x00\x00bq\x03X\x01\x00\x00\x002q\x04X\x01\x00\x00\x00cq\x05X\x01\x00\x00\x003q\x06u.'
{'a': '1', 'b': '2', 'c': '3'}
9.183143498391086
a = {'a': '1', 'b': '2', 'c': '3'}

习题2:
使用两种方法计算给定列表中list(range(10000))中, 和为10000的两个数, 输出每对数的对应两个下标
并使用 time, timeit, profile 三种方法测试效率
"""
习题2:
使用两种方法计算给定列表中list(range(10000))中,
和为10000的两个数,输出每对数的对应两个下标
并使用 time, timeit, profile 三种方法测试效率
这里全部按照执行100000次比较
"""
from timeit import timeit
from time import time
import profile


#方法一
#time()单独使用仅仅执行一次
start = time()
# a = list(range(10000))
b = {}
for i in range(50000):
    b[i+1]=100000-i+1
# print(b)
end = time()
print("time = %.4f s"%(end-start))

#timeit()执行1000000次,转化
print("timeit = %.6f s"%float(timeit("""
b = {}
for i in range(3):
    b[i+1]=3-i+1
""",globals=globals())/10)#1百万次除10
)
#profile()执行一次
def sum_num(n):
    b = {}
    for i in range(int(n/2)):
        if i + 1 not in b:
            b[i + 1] = n - i + 1
    return b
profile.run("sum_num(100000)",sort="ncall")

#方法二、
#time方法
start = time()
a = list(range(50000))
b = {}
for i in a:
    if i+1 not in b:
        b[i + 1] = 100000 - i + 1
end = time()
print("time2 = %.4f s"%(end-start))
# print(b)

#timeit方法
print("timeit2 = %.6f s"%float(
      timeit("""
a = list(range(3))
b = {}
for i in a:
    b[i + 1] = 3 - i + 1
    """,globals=globals())/10)
)

#profile方法
def sum_numb(n):
    a = list(range(int(n/2)))
    b = {}
    for i in a:
        b[i + 1] = n - i + 1
    return b
profile.run("sum_numb(100000)",sort="ncall")
>>
time = 0.0100 s
timeit = 0.051264 s
         5 function calls in 0.016 seconds

   Ordered by: call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.016    0.016 profile:0(sum_num(100000))
        1    0.000    0.000    0.016    0.016 :0(exec)
        1    0.000    0.000    0.016    0.016 <string>:1(<module>)
        1    0.016    0.016    0.016    0.016 Temporary File.py:65(sum_num)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        0    0.000             0.000          profile:0(profiler)


time2 = 0.0140 s
timeit2 = 0.067639 s
         5 function calls in 0.016 seconds

   Ordered by: call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.016    0.016 profile:0(sum_numb(100000))
        1    0.000    0.000    0.016    0.016 :0(exec)
        1    0.000    0.000    0.016    0.016 <string>:1(<module>)
        1    0.016    0.016    0.016    0.016 Temporary File.py:96(sum_numb)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        0    0.000             0.000          profile:0(profiler)

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页