Viewing a response to: @profitgenerator/python-code-speed-improvement
I can't agree with what you wrote in "Style vs Performance". Your example seems like a classic example of [premature optimization](http://wiki.c2.com/?PrematureOptimization). Especially this sentence: > whenever you can, and style doesn’t matter Style *matters a lot*. Assuming that what you write is not a single-use program that'll be forgotten after execution, your code will be read much, much more often than written or refactored. This is even more true when you collaborate with others - then you can either focus on style first, or write terribly styled code and spend thrice as time on writing comments or docs, or be a jerk to your coworkers and write just the terribly styled code. And as for your example, I measured it: ``` $ python -m timeit 'array=[1,1,1,1,1,1,1,1,1,1] sum=0 for a in range(0,10): sum+=array[a]' 1000000 loops, best of 3: 0.802 usec per loop $ python -m timeit 'array=[1,1,1,1,1,1,1,1,1,1] sum=array[0]+array[1]+array[2]+array[3]+array[4]+array[5]+array[6]+array[7]+array[8]+array[9]' 1000000 loops, best of 3: 0.333 usec per loop ``` **0.802µs vs 0.333µs**. Yeah, it's slower. Does it matter? **Most likely not**. What matters is that second example is much less readable and more error-prone (which you even admit yourself).
author | modrzew |
---|---|
permlink | re-profitgenerator-python-code-speed-improvement-20171119t140334970z |
category | python |
json_metadata | {"tags":["python"],"links":["http://wiki.c2.com/?PrematureOptimization"],"app":"steemit/0.1"} |
created | 2017-11-19 14:03:36 |
last_update | 2017-11-19 14:03:36 |
depth | 1 |
children | 2 |
last_payout | 2017-11-26 14:03:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,267 |
author_reputation | 512,745,973,730 |
root_title | "Python Code Speed Improvement" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 20,879,607 |
net_rshares | 1,989,129,114 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
profitgenerator | 0 | 1,989,129,114 | 100% | ||
dchad | 0 | 0 | 100% |
Yes but it matters to me though, I was analyzing the market and speed is very crucial, a default calculation takes like 2 hours. Besides the long string can be automatically written too: ``` string="sum=array[0]" for i in range(1,9+1): string+="+array["+str(i)+"]" print (string) ``` This way we avoid typos and mistakes.
author | profitgenerator |
---|---|
permlink | re-modrzew-re-profitgenerator-python-code-speed-improvement-20171121t224317200z |
category | python |
json_metadata | {"tags":["python"],"app":"steemit/0.1"} |
created | 2017-11-21 22:43:33 |
last_update | 2017-11-21 22:43:33 |
depth | 2 |
children | 1 |
last_payout | 2017-11-28 22:43:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 332 |
author_reputation | 68,549,319,463,075 |
root_title | "Python Code Speed Improvement" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 21,138,611 |
net_rshares | 0 |
...and then you'd have to `eval` that long string to get an array which would be even slower, not to say completely unsafe 😉 If your calculation takes 2 hours, then I'd say replacing `for` loops with manually typed array accesses is not the best place to optimize. Most likely something else takes long to finish. Have you tried [profiling your code](https://docs.python.org/3/library/profile.html)?
author | modrzew |
---|---|
permlink | re-profitgenerator-re-modrzew-re-profitgenerator-python-code-speed-improvement-20171122t005750495z |
category | python |
json_metadata | {"tags":["python"],"links":["https://docs.python.org/3/library/profile.html"],"app":"steemit/0.1"} |
created | 2017-11-22 00:57:51 |
last_update | 2017-11-22 00:57:51 |
depth | 3 |
children | 0 |
last_payout | 2017-11-29 00:57:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 401 |
author_reputation | 512,745,973,730 |
root_title | "Python Code Speed Improvement" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 21,147,682 |
net_rshares | 0 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
dchad | 0 | 0 | 100% |