I have optimization's problem with this for looping code:
My code like this:
total = 0
result = []
for test in testing:
total += test.amount
result.append({'category': test.category, 'value': test.amount})
i need to optimization the code to one line. i trying to change code to
total = 0
result = [ {'category': test.category, 'value': test.amount} for test in testing]
but i can't optimization for operation total
Thanks for help.
I timed a couple variations for you to compare:
from functime import functime
class test_class:
def __init__(self, amt=10, cat='A'):
self.amount = amt
self.category = cat
def func_1(testing):
total = 0
result = []
for test in testing:
total += test.amount
result.append({'category': test.category, 'value': test.amount})
return
def func_2(testing):
result = [{'category': test.category, 'value': test.amount} for test in testing]
total = sum([i['value'] for i in result])
return
def func_3(testing):
result = [{'category': test.category, 'value': test.amount} for test in testing]
total = sum([test.amount for test in testing])
return
def func_4(testing):
result, totals = [], []
for test in testing:
totals.append(test.amount)
result.append({'category': test.category, 'value': test.amount})
total = sum(totals)
return
def func_5(testing):
result = [{'category': test.category, 'value': test.amount} for test in testing]
total = sum(test.amount for test in testing)
return
I'll omit the calling of functions and print statements to save space:
--------------------
10 variables / 10000 iterations
--------------------
func_1: 0.0898552949414
func_2: 0.0572853889704
func_3: 0.0666673211647
func_4: 0.0676401432815
func_5: 0.0496420416234
--------------------
100 variables / 10000 iterations
--------------------
func_1: 0.371173584934
func_2: 0.310192364417
func_3: 0.330012053177
func_4: 0.53144825992
func_5: 0.377762000408
--------------------
1000 variables / 10000 iterations
--------------------
func_1: 3.60984478132
func_2: 3.05880308072
func_3: 3.29883265808
func_4: 4.98255212296
func_5: 3.36148284866
Some interesting observations about your code:
total
and building the result
list are independent native python constructs
result = [{'category': test.category, 'value': test.amount} for test in testing] total = sum(d['value'] for d in result)
Editted to show that total is obtained by looping over a list of dicts and summing the value
keys of each dict.
By the way , both lines are optimised
python constructs.
I figured out how to do it all on one line, as you'd asked for. This is definitely not the way to go, but here are a couple of options:
def one_line():
return [(a[0][0], a[1]) for a in [ (t, [tuple(t.append(t.pop()+test.amount) or te for te in [{'category': test.category, 'value': test.amount}])[0] for test in testing ]) for t in [[0]] ] ][0]
def one_line_alternative():
return next(map(lambda a: (a[0].pop(),a[1]), [ (t, list(map(lambda test: next((t.append(t.pop()+test.amount)) or te for te in [{'category': test.category, 'value': test.amount}]), testing)) ) for t in [[0]] ]))
And here is a comparison to Evan Nowak fastest answer. I'm using 10 variables, 50000 iterations, and Evan Nowak's fastest optimization for 10 variables.
One Line: 2.8529339203163353
One Line Alternative: 2.859311107199918
Evan Nowak's fastest optimization: 2.3440381323539983
So, as expected, putting everything on one line is not a good idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.