Найти - Пользователи
Полная версия: functools и оптимизация
Начало » Python для новичков » functools и оптимизация
1
alexx11
Вот эти (map, filter, в частности reduce) функции вообще только для красоты или или от них есть практическая польза?
>>>
>>> a = []
>>> for i in range(2000):
... a.append(set(range(i)))
...
>>> def f1(a):
... s = set()
... for aa in a : s |= aa
...
>>> def f2(a):
... s = reduce(set.union, a)
...
>>> import cProfile
>>> cProfile.run('f1(a)')
3 function calls in 0.420 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.420 0.420 0.420 0.420 <stdin>:1(f1)
1 0.000 0.000 0.420 0.420 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}


>>> cProfile.run('f2(a)')
4 function calls in 1.150 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.150 1.150 <stdin>:1(f2)
1 0.000 0.000 1.150 1.150 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 1.150 1.150 1.150 1.150 {reduce}


>>>
Без комментариев.
Isem
Ну если выполняемая бинарная функция заранее неизвестна (вместо set.union), например, то reduce - вполне подходящее короткое решение.
alexx11
Isem
Ну если выполняемая бинарная функция заранее неизвестна
Хмм… Да, пожалуй.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB