create account

RE: Python Code Speed Improvement by modrzew

View this thread on: hive.blogpeakd.comecency.com

Viewing a response to: @profitgenerator/python-code-speed-improvement

· @modrzew ·
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).
👍  ,
properties (23)
authormodrzew
permlinkre-profitgenerator-python-code-speed-improvement-20171119t140334970z
categorypython
json_metadata{"tags":["python"],"links":["http://wiki.c2.com/?PrematureOptimization"],"app":"steemit/0.1"}
created2017-11-19 14:03:36
last_update2017-11-19 14:03:36
depth1
children2
last_payout2017-11-26 14:03:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,267
author_reputation512,745,973,730
root_title"Python Code Speed Improvement"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id20,879,607
net_rshares1,989,129,114
author_curate_reward""
vote details (2)
@profitgenerator ·
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.
properties (22)
authorprofitgenerator
permlinkre-modrzew-re-profitgenerator-python-code-speed-improvement-20171121t224317200z
categorypython
json_metadata{"tags":["python"],"app":"steemit/0.1"}
created2017-11-21 22:43:33
last_update2017-11-21 22:43:33
depth2
children1
last_payout2017-11-28 22:43:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length332
author_reputation68,549,319,463,075
root_title"Python Code Speed Improvement"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id21,138,611
net_rshares0
@modrzew ·
...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)?
👍  
properties (23)
authormodrzew
permlinkre-profitgenerator-re-modrzew-re-profitgenerator-python-code-speed-improvement-20171122t005750495z
categorypython
json_metadata{"tags":["python"],"links":["https://docs.python.org/3/library/profile.html"],"app":"steemit/0.1"}
created2017-11-22 00:57:51
last_update2017-11-22 00:57:51
depth3
children0
last_payout2017-11-29 00:57:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length401
author_reputation512,745,973,730
root_title"Python Code Speed Improvement"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id21,147,682
net_rshares0
author_curate_reward""
vote details (1)