Ipython模块memory_profiler中的错误,%mprun

时间:2019-05-12 08:28:34

标签: python memory equality memory-profiling syntax-checking

如您所见,以下代码的结果是不公平的,因为它不可能是迭代器与“ return”或“ if y:”花费相同的时间。为什么?我该如何解决?

在某些测试中,我的代码太重了,因此我尝试检查每一行的“内存支出”。

    def hmd(num):
        if num==1:
            return 1
        def resheto(num):
            t = list(range(num+1))
            t[1]=0
            for i in t:
                if t[i]:
                    for u in range(i**2, num+1, i):
                        t[u]=0
            end = [i for i in range(2,num+1) if t[i]]
            end = list(filter(lambda x: num%x==0, end))
            return end
        l = resheto(num)
        def end(num, l, minw, u=[]):
            u.append(minw)
            num=num//minw
            if num==1:
                return u
            ent = list(filter(lambda x: num%x==0, l))
            minw=ent[0]
            return end(num, l, minw, u)
        o = end(num, l, l[0])
        def hi(o, j=[]):
            if len(o)==0:
                return j
            y = len(list(filter(lambda x: o[0]==x, o)))
            u = list(filter(lambda x: o[0]!=x, o))
            if y:
                j.append('{p}^{y}'.format(p=o[0], y=y))
            else:
                j.append(str(o[0]))
            return hi(u, j)
        return ('*'.join(hi(o))).replace('^1','')
    from dgdgd_zooz_memory import hmd
    %mprun -f hmd hmd(100)

---结果---

Filename: /Users/Pro/Desktop/python/sirius/dgdgd_zooz_memory.py

第#行,内存使用量增加行内容

 1     56.5 MiB     56.5 MiB   def hmd(num):
 2     56.5 MiB      0.0 MiB       if num==1:
 3                                     return 1
 4     56.5 MiB      0.0 MiB       def resheto(num):
 5     56.5 MiB      0.0 MiB           t = list(range(num+1))
 6     56.5 MiB      0.0 MiB           t[1]=0
 7     56.5 MiB      0.0 MiB           for i in t:
 8     56.5 MiB      0.0 MiB               if t[i]:
 9     56.5 MiB      0.0 MiB                   for u in range(i**2, num+1, i):
10     56.5 MiB      0.0 MiB                       t[u]=0
11     56.5 MiB      0.0 MiB           end = [i for i in range(2,num+1) if t[i]]
12     56.5 MiB      0.0 MiB           end = list(filter(lambda x: num%x==0, end))
13     56.5 MiB      0.0 MiB           return end
14     56.5 MiB      0.0 MiB       l = resheto(num)
15     56.5 MiB      0.0 MiB       def end(num, l, minw, u=[]):
16     56.5 MiB      0.0 MiB           u.append(minw)
17     56.5 MiB      0.0 MiB           num=num//minw
18     56.5 MiB      0.0 MiB           if num==1:
19     56.5 MiB      0.0 MiB               return u
20     56.5 MiB      0.0 MiB           ent = list(filter(lambda x: num%x==0, l))
21     56.5 MiB      0.0 MiB           minw=ent[0]
22     56.5 MiB      0.0 MiB           return end(num, l, minw, u)
23     56.5 MiB      0.0 MiB       o = end(num, l, l[0])
24     56.5 MiB      0.0 MiB       def hi(o, j=[]):
25     56.5 MiB      0.0 MiB           if len(o)==0:
26     56.5 MiB      0.0 MiB               return j
27     56.5 MiB      0.0 MiB           y = len(list(filter(lambda x: o[0]==x, o)))
28     56.5 MiB      0.0 MiB           u = list(filter(lambda x: o[0]!=x, o))
29     56.5 MiB      0.0 MiB           if y:
30     56.5 MiB      0.0 MiB               j.append('{p}^{y}'.format(p=o[0], y=y))
31                                     else:
32                                         j.append(str(o[0]))
33     56.5 MiB      0.0 MiB           return hi(u, j)
34     56.5 MiB      0.0 MiB       return ('*'.join(hi(o))).replace('^1','')

0 个答案:

没有答案