Python-';地图';结果慢于';对于';设置相同的对象列表

Python-';地图';结果慢于';对于';设置相同的对象列表,python,list,dictionary,comparison,settings,Python,List,Dictionary,Comparison,Settings,我正在寻找一种快速的方法来设置一个大的对象列表。我知道“map”应该比“for”快,但在我的测试中没有 我还试图了解创建新对象或设置现有对象是否更快 创建一个新对象似乎比设置它要快 有人能解释为什么会这样吗?多谢大家 import time import numpy as np class myc(): def __init__(s,_v): s.v = _v def set(s,v): s.v = v return s de

我正在寻找一种快速的方法来设置一个大的对象列表。我知道“map”应该比“for”快,但在我的测试中没有


我还试图了解创建新对象或设置现有对象是否更快

创建一个新对象似乎比设置它要快

有人能解释为什么会这样吗?多谢大家

import time
import numpy as np

class myc():
    def __init__(s,_v):
        s.v = _v

    def set(s,v):
        s.v = v
        return s

def e_set(e):
    e.set( 10000 + np.random.randint(10000,size=3))
    return e


n   = 10**5
rng = range(n)

ta1=time.time()
a = [ myc(np.random.randint(10000,size=3)) for i in rng ]
ta2=time.time()
print("R1: created 'a' with new ", n , "objects with random values in " , ta2-ta1, " seconds")


tb1=time.time()
for e in a: e.set(np.random.randint(10000,size=3))
tb2=time.time()
print("R2: setting 'a' objects with random values in " , tb2-tb1, " seconds")


tc1=time.time()
m = map(e_set, a)
tc2=time.time()
print("R3: mapped e_set,'a' -> 'm'  in " , tc2-tc1, " seconds")
print("R4: values of 'a' > 10000 ? : ", a[5000].v , a[5000].v > np.array([10000,10000,10000])   )


td1=time.time()
lm = list(m)
td2=time.time()
print("R5: list('m') -> 'lm' in" , td2-td1, " seconds")
print("R6: values of 'lm' > 10000 ? : ", lm[5000].v , lm[5000].v > np.array([10000,10000,10000])  )
print("R7: values of 'a' > 10000 ? : ", a[5000].v , a[5000].v > np.array([10000,10000,10000])  )
print("R8: lm[x] == a[x] ? ", lm[5000],a[5000])
结果:

R1: created 'a' with new  100000 objects with random values in  0.811359167098999  seconds
R2: setting 'a' objects with random values in  0.8541171550750732  seconds
R3: mapped e_set,'a' -> 'm'  in  3.0994415283203125e-06  seconds
R4: values of 'a' > 10000 ? :  [8444 4830 6574] [False False False]
R5: list('m') -> 'lm' in 1.0371909141540527  seconds
R6: values of 'lm' > 10000 ? :  [16584 11666 19657] [ True  True  True]
R7: values of 'a' > 10000 ? :  [16584 11666 19657] [ True  True  True]
R8: lm[x] == a[x] ?  <__main__.myc object at 0x7fbfcfa60320> <__main__.myc object at 0x7fbfcfa60320>
这里是新的结果

R1: created 'a' with new  100000 objects with random values in  0.8763055801391602  seconds
R2: setting 'a' objects with random values in  0.8311254978179932  seconds
R3: mapped e_set,'a' -> 'm'  in  1.430511474609375e-06  seconds
R4: values of 'a' > 10000 ? :  [9557 1359 5255] [False False False]
R5: list('m') -> 'lm' in 1.031069040298462  seconds
R6: values of 'lm' > 10000 ? :  [12996 17379 18531] [ True  True  True]
R7: values of 'a' > 10000 ? :  [12996 17379 18531] [ True  True  True]
R8: lm[x] == a[x] ?  <__main__.myc object at 0x7fd64badd390> <__main__.myc object at 0x7fd64badd390>
---------------------------------------------------
R9: set_objects(a) in 0.8176069259643555  seconds
---------------------------------------------------
R1:在0.8763055801391602秒内创建了一个包含100000个随机值的新对象的“a”
R2:在0.8311254978179932秒内使用随机值设置“a”对象
R3:1.430511474609375e-06秒内映射的e_集,'a'->'m'
R4:“a'>10000”的值:[95571359525][假]
R5:列表('m')->'lm'只需1.031069040298462秒
R6:“lm'>10000”的值:[129961737918531][正确]
R7:'a'>10000?的值:[129961737918531][正确]
R8:lm[x]==a[x]?
---------------------------------------------------
R9:在0.8176069259643555秒内设置_对象(a)
---------------------------------------------------

是的,R9似乎比R1R2

快。我知道“map”应该比“for”快。不,你不知道,因为没有理由相信,因为它通常不是真的。注意,
R2
如果你在函数中做的话会更快,如果你是这样做的话,这可能是最快的方法。“我还试图了解创建新对象或设置现有对象是否更快。”设置属性通常比创建整个新对象更快。@juanpa.arrivillaga你能告诉我如何在R2过程中使用函数以获得更快的结果吗?”通常,设置属性的速度比我假设的要快,但在本例中似乎不是这样。一个关于如何设置更快的对象大列表的例子将会很受欢迎,我的意思是把代码放在一个函数中:
def set\u objects(list\u of \u myc):对于对象列表中的x:e.set(np.random.randint(10000,size=3)
然后是time
set\u objects(a)
R1: created 'a' with new  100000 objects with random values in  0.8763055801391602  seconds
R2: setting 'a' objects with random values in  0.8311254978179932  seconds
R3: mapped e_set,'a' -> 'm'  in  1.430511474609375e-06  seconds
R4: values of 'a' > 10000 ? :  [9557 1359 5255] [False False False]
R5: list('m') -> 'lm' in 1.031069040298462  seconds
R6: values of 'lm' > 10000 ? :  [12996 17379 18531] [ True  True  True]
R7: values of 'a' > 10000 ? :  [12996 17379 18531] [ True  True  True]
R8: lm[x] == a[x] ?  <__main__.myc object at 0x7fd64badd390> <__main__.myc object at 0x7fd64badd390>
---------------------------------------------------
R9: set_objects(a) in 0.8176069259643555  seconds
---------------------------------------------------