Mein Favorit wäre dies:Sirius3 hat geschrieben:Code: Alles auswählen
result = [0, 0] for n in kassa: result[n<0] += n
Code: Alles auswählen
p = sum(i for i in kassa if i > 0)
n = sum(kassa) - p
Mein Favorit wäre dies:Sirius3 hat geschrieben:Code: Alles auswählen
result = [0, 0] for n in kassa: result[n<0] += n
Code: Alles auswählen
p = sum(i for i in kassa if i > 0)
n = sum(kassa) - p
Code: Alles auswählen
import random
import sys
import time
if sys.version.startswith('3'):
from functools import reduce
else:
range = xrange
numbers = list(range(100000)) + list(range(-100000, 0))
random.shuffle(numbers)
def with_filter():
t = time.time()
sum(filter(lambda e: e > 0, numbers))
sum(filter(lambda e: e < 0, numbers))
return time.time() - t
def with_map():
t = time.time()
map(sum, zip(*((0, item) if item < 0 else (item, 0) for
item in numbers)))
return time.time() - t
def with_lc():
t = time.time()
sum(i for i in numbers if i<0)
sum(i for i in numbers if i>0)
return time.time() - t
def with_reduce():
t = time.time()
reduce(lambda pos_neg, next: (pos_neg[0] + next, pos_neg[1]) if
next >= 0 else (pos_neg[0], pos_neg[1] + next),
numbers, (0, 0))
return time.time() - t
def with_sub():
t = time.time()
p = sum(i for i in numbers if i > 0)
sum(numbers) - p
return time.time() - t
def with_for():
t = time.time()
result = [0, 0]
for n in numbers:
result[n<0] += n
return time.time() - t
def start_test(count):
t_filter, t_map, t_lc, t_reduce, t_sub, t_for = 0, 0, 0, 0, 0, 0
for i in range(count):
t_filter += with_filter()
t_map += with_map()
t_lc += with_lc()
t_reduce += with_reduce()
t_sub += with_sub()
t_for += with_for()
print ('filter: {0}\n'
'map : {1}\n'
'lc : {2}\n'
'reduce: {3}\n'
'sub : {4}\n'
'for : {5}'.format(t_filter / count,
t_map / count,
t_lc / count,
t_reduce / count,
t_sub / count,
t_for / count))
if __name__ == '__main__':
start_test(int(sys.argv[1]))
Code: Alles auswählen
$ python ./measure_sum.py 100
filter: 0.0847445440292
map : 0.523688633442
lc : 0.0519872546196
reduce: 0.0740648984909
sub : 0.0361274647713
for : 0.0377649760246
Code: Alles auswählen
python3 ./measure_sum.py 100
filter: 0.10626415252685546
map : 0.3320684552192688
lc : 0.06279780626296998
reduce: 0.08097020864486694
sub : 0.04472310304641724
for : 0.04908294916152954
Nö, jeweils 100 mal.BlackJack hat geschrieben:... Du scheinst das nur einmal aufzurufen. ...
Code: Alles auswählen
def start_test(count):
functions = [func for name,func in globals().iteritems() if name.startswith('with_')]
times = [0]*len(functions)
for i in range(count):
for idx, func in enumerate(functions):
times[idx] += func()
print '\n'.join('{0:6s}: {1}'.format(func.__name__.lstrip('with_'),t/count) for t,func in zip(times,functions))
Code: Alles auswählen
>>> def neg_pos(it):
... n, p = 0, 0
... for el in it:
... if el<0:
... n += el
... else:
... p += el
... return n, p
Code: Alles auswählen
def start_test(count=5, repeat=3):
functions = [func for name, func in globals().items() if
name.startswith('with_')]
result = {}
timer = time.time
for func in functions:
import_ = 'from __main__ import {0}'.format(func.__name__)
func_ = '{0}()'.format(func.__name__)
results = timeit.repeat(stmt=func_, setup=import_, timer=timer,
repeat=repeat, number=count)
result[func.__name__] = min(results), max(results)
result = sorted(result.items(), key=lambda e: e[0])
print('{0} repetitions of {1} runs:'.format(repeat, count))
print('\t\tmin\t\tmax')
for func, times in result:
print('{func}:\t{times[0]:f}\t{times[1]:f}'.format(times=times,
func=func))
if __name__ == '__main__':
args = map(int, sys.argv[1:3])
start_test(*args)
Code: Alles auswählen
$ python ./measure_sum.py 10 5
5 repetitions of 10 runs:
min max
with_filter: 0.856682 0.864453
with_for: 0.387800 0.404242
with_ifelse: 0.282156 0.295538
with_lc: 0.529992 0.540703
with_map: 1.683503 1.728494
with_reduce: 0.761827 0.793188
with_sub: 0.369928 0.388947
Code: Alles auswählen
$ python3 ./measure_sum.py 10 5
5 repetitions of 10 runs:
min max
with_filter: 1.111608 1.166339
with_for: 0.493963 0.498310
with_ifelse: 0.407400 0.417205
with_lc: 0.630200 0.632272
with_map: 1.335373 1.345122
with_reduce: 0.807354 0.809970
with_sub: 0.453200 0.454332
Ich will einfach auch mal etwas entwickelt haben, das sowas wie einen "Start"-Button hat...Sirius3 hat geschrieben:Der Name start_test ist auch etwas irreführend. da der Test gestartet durchgeführt und beendet wird.