为什么Python3要把map函数改成了map class?费解

31 views
Skip to first unread message

lee Alexander

unread,
Jan 12, 2011, 5:25:40 AM1/12/11
to pyth...@googlegroups.com
我做了个测试,2.6中map函数遍历数组的速度比for循环快,为啥Python3中要去掉map函数,改成map类,经过测试改成map类后实现相同的功能比map函数满得多。
而且经过测试,Python3.1的for循环也比2.6来得慢,真是郁闷,怎么越整越回去了呢?

--
Alexander.Li
+86 15308006505
mail: superp...@gmail.com/superp...@hotmail.com
site:http://alexander-lee.cnblogs.com

victor lee

unread,
Jan 12, 2011, 8:30:41 AM1/12/11
to pyth...@googlegroups.com
now, in 3.1, map is lazy

2011/1/12 lee Alexander <superp...@gmail.com>
--
来自: python-cn`CPyUG`华蟒用户组(中文Python技术邮件列表)
发言: pyth...@googlegroups.com
退订: python-cn+...@googlegroups.com (向此发空信即退!)
详情: http://code.google.com/p/cpyug/wiki/PythonCn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp

lee Alexander

unread,
Jan 12, 2011, 8:04:58 PM1/12/11
to pyth...@googlegroups.com
lazy了有什么好处呢,为了Lazy一下效率降低太多了,很显然划不来,Python的函数既然已经是对象可以当参数传递了,那么传递函数对象好了,干嘛非要把map干掉呢?

zhao shichen

unread,
Jan 12, 2011, 9:10:41 PM1/12/11
to pyth...@googlegroups.com
我也不理解,本来很好用的东西,给阉割了,就好像java对c++的阉割一样.
呆痴木讷,君子四德

victor lee

unread,
Jan 12, 2011, 9:23:19 PM1/12/11
to pyth...@googlegroups.com
  • First of all, Lazy is great when you wont use all the items at once or you are going to produce the thing one by one later
For Example
l=map(lambda x:do_some_func,list_)
the map wont do anything till you visit it, for example
for i in l :
or maybe
x=list(l)
 
  • Secondly, map(func,iterable) is now similar to ( func(i) for i in iterable ), which is a generator instead of a list.
in py2, there, map is equals to [f(i) for i in x] which in py3 has the same effects.
Noticing that map is a bit slower than the [ f(i) for i in x ] as it is using funcs, and also in the below case , [ f(I) for i in x ] is much faster
map( lambda x:x+1,l)  # slower than [i+1 for i in x]
that is because python will first create a lambda

  • Last but not least, in my opinion not everyone know the syntax of ( f(i) for i in x if condition(i) ), so there is a Map class to avoid the speed wasting
  • PS : if you want just "faster" py2 style map, use [f(i) for i in l] instead.
sorry for bothering you using my poor english as my Input Method is broken
Best Regards
VIC
100.1.13

2011/1/13 lee Alexander <superp...@gmail.com>

lee Alexander

unread,
Jan 12, 2011, 10:23:17 PM1/12/11
to pyth...@googlegroups.com
我重新测试了一次,又发现了一点奇怪的现象,按照 lambda x:x+1的方式测试map确实慢了很多,但是我测试的时候图方便,直接使用了str这个buildin函数,结果map就比for迭代和列表解析快,为了方便表达我直接贴代码了

ls=range(100000)

def loop1():
    for i in ls:
        x=i+1

def loop2():
    map(lambda x:x+1,ls)

def loop3():
    [i+1 for i in ls]

结果是
[0.29097795486450195, 0.30311298370361328, 0.28920412063598633]
[0.96499300003051758, 0.94327902793884277, 0.97041106224060059]
[0.34845209121704102, 0.33587312698364258, 0.34033107757568359]

map完败

但是我换成昨天实验的代码

ls=range(100000)

def loop1():
    for i in ls:
        str(i)

def loop2():
    map(str,ls)

def loop3():
    [str(i) for i in ls]

结果:
[1.5978419780731201, 1.5836057662963867, 1.6044631004333496]
[1.5450370311737061, 1.5522379875183105, 1.3477730751037598]
[2.1679210662841797, 1.9605419635772705, 2.2622189521789551]


str和map有什么不可告人的秘密?

victor lee

unread,
Jan 12, 2011, 10:29:00 PM1/12/11
to pyth...@googlegroups.com
dont use time to have the timer
try timeit.
i think the loop you run is too small?

2011/1/13 lee Alexander <superp...@gmail.com>

victor lee

unread,
Jan 12, 2011, 10:29:22 PM1/12/11
to pyth...@googlegroups.com
import timeit;
help(timeit) for documents

2011/1/13 victor lee <victor...@gmail.com>

lee Alexander

unread,
Jan 13, 2011, 2:32:01 AM1/13/11
to pyth...@googlegroups.com
我就是timeit测试的

shhgs

unread,
Jan 13, 2011, 8:01:40 AM1/13/11
to pyth...@googlegroups.com
lazy这都是跟FP学的。

至于你的这个所谓的性能测试,没有任何意义。这点性能的差别,不是因为map,是因为int和str的区别。

lazy有lazy的好处。

import itertools
seed = itertools.count()
f = map(lambda x : pow(x, 2), seed)

for i in f:
if i >= 100:
break
print(i)

> >>> 退订: python-cn+...@googlegroups.com<python-cn%2Bunsu...@googlegroups.com>(向此发空信即退!)


> >>> 详情: http://code.google.com/p/cpyug/wiki/PythonCn
> >>> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
> >>>
> >>
> >>
> > --
> > 来自: python-cn`CPyUG`华蟒用户组(中文Python技术邮件列表)
> > 发言: pyth...@googlegroups.com

> > 退订: python-cn+...@googlegroups.com<python-cn%2Bunsu...@googlegroups.com>(向此发空信即退!)

pansz

unread,
Jan 14, 2011, 1:56:26 AM1/14/11
to pyth...@googlegroups.com
2011/1/13 victor lee <victor...@gmail.com>:

> sorry for bothering you using my poor english as my Input Method is broken
> Best Regards
> VIC
> 100.1.13

qq and sogou all have cloud input written in AJAX, you got input
method provide that you have a decent web-browser.

Reply all
Reply to author
Forward
0 new messages