4

As you can see, result of the code below is unfair, because it can't be that iterator spent same time as "return" or "if y:". Why is it? How can I fix it?

In some tests my code is too heavy so I tried to check "memory-spend" of an each row.

    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)

---result---

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

Line # Mem usage Increment Line Contents

 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','')
sanyassh
  • 8,100
  • 13
  • 36
  • 70
arseniyx92
  • 41
  • 1

0 Answers0