简体   繁体   中英

Optimising list creation in Python

I am creating a list by using

sum_range=list(2**x for x in range(start_range,end_range+1))

Here the start_range and the end_range take large values from 0 to about 4 million.

This line of code seems to be the bottle neck. Ultimately I want the sum of all the number in the list sum_range. There can possibly be a better way to do this?

Time take when:-

start_range=188640 end_range=197280

The code takes about 6 seconds to run on my system.

The sum over 2**n is known algebraically. For n=0 to m it is : 2**(m+1)-1 . So if you want to know it from a to b (inclusive) take 2**(b+1) - 2**(a+1) + 2**a . Look up geometric series for the general case.

Try using "<<" operator instead of "**" , it also will save computing time greatly from 4.0~5.0 seconds to 0.07~0.33 seconds .

import datetime

t_start_1 = datetime.datetime.utcnow()
sum_range_1 = list(1<<x for x in range(188640,197280))
t_end_1 = datetime.datetime.utcnow()
print "using '<<' cost: " + str(t_end_1 - t_start_1) + "seconds"

t_start_2 = datetime.datetime.utcnow()
sum_range_2 = list(2**x for x in range(188640,197280))
t_end_2 = datetime.datetime.utcnow()
print "using '**' cost: " + str(t_end_2 - t_start_2) + "seconds"

output:

using '<<' cost: 0:00:00.327000 seconds
using '**' cost: 0:00:04.990000 seconds

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM