什么时候应该使用Map而不是For循环? [英] When should I use a Map instead of a For Loop?

查看:498
本文介绍了什么时候应该使用Map而不是For循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与以下内容有关:(在Python代码中)

 对于i中的对象:
doSomething i)



  map(doSomething,object)

这两个都很容易理解,但有没有速度差异?现在,如果doSomething有一个返回值,我们需要检查它是否会从map中返回,在for循环中,我们可以创建自己的列表或者一次检查一个。



pre $ 为我在对象中:
returnValue = doSomething(i)
doSomethingWithReturnValue(returnValue)



  returnValue = map(doSomething,object) 
map(doSomethingWithReturnValue,returnValue)

现在我感觉到这两个分歧了。这两个doSomethingWithReturnValue函数可能有所不同,如果我们在循环中检查它们,或者在最后一次检查它们时会产生不同的结果。此外,似乎for循环将始终工作,也许更慢,地图只能在某些情况下工作。当然,我们可以做出扭曲的工作,但总的来说,是要避免这种工作。



我在找的是一个场景,映射功能与在性能,可读性,可维护性或实现速度方面做得比较好,如果答案是真的没有什么大的差别,那么我想知道在实践中人们使用哪一种或者哪一种是真正完全武断的,并且根据您的机构通过编码标准来设置。


$ b $ p

感谢!

解决方案

map 当您想要将函数应用于迭代器的每个项目并返回结果列表时,它非常有用。这比使用for循环和构造列表更简单,更简洁。



对于其他情况,在lisp里面有很多基本上使用宏和map编写的迭代结构。因此,在 map 不适合的情况下,对循环使用



理论上,如果我们有一个足够聪明的编译器/解释器来使用多个CPU /处理器,那么可以更快地实现 map 每个项目的不同操作可以并行进行。然而,我认为目前情况并非如此。

This is relating to the following: (In Python Code)

for i in object:
     doSomething(i)

versus

map(doSomething, object)

Both are easy to understand, and short, but is there any speed difference? Now, if doSomething had a return value we needed to check it would be returned as a list from map, and in the for loop we could either create our own list or check one at a time.

for i in object:
     returnValue = doSomething(i)
     doSomethingWithReturnValue(returnValue)

versus

returnValue = map(doSomething, object)
map(doSomethingWithReturnValue, returnValue)

Now, I feel the two diverge a little bit. The two doSomethingWithReturnValue functions may be different based on if checking them on the fly as we go through the loop or if checking them all at once at the end produce different results. Also it seems the for loop would always work, maybe slower, where the map would only work under certain scenarios. Of course, we could make contortions to make either work, but the whole point is to avoid this type of work.

What I'm looking for is a scenario where the mapping function truly shines in comparison to a well done for loop in performance, readability, maintainability, or speed of implementation. If the answer is there really isn't a big difference then I'd like to know when in practice people use one or the other or if it's really completely arbitrary and set by coding standards depending on your institution.

Thanks!

解决方案

map is useful when you want to apply the function to every item of an iterable and return a list of the results. This is simpler and more concise than using a for loop and constructing a list.

for is often more readable for other situations, and in lisp there were lots of iteration constructs that were written basically using macros and map. So, in cases where map doesn't fit, use a for loop.

In theory, if we had a compiler/interpreter that was smart enough to make use of multiple cpus/processors, then map could be implemented faster as the different operations on each item could be done in parallel. I don't think this is the case at present, however.

这篇关于什么时候应该使用Map而不是For循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆