Improving sum() calculations on evenly spaced list
I have a snippet where I need to have 10 Million evenly spaced number between 0 and 1. I have a logic function which takes care to select a random index and return the sum of numbers from that index to the end of the list. So the snippet looks like this: import random import numpy as np ten_million = np.linspace(0.0, 1.0, 10000000) def deep_dive_logic(): # this pick is derived from good logic, however, let's just use random here for demonstration pick = random.randint(0, 10000000) return sum(ten_million[pick:]) for _ in range(2500): r = deep_dive_logic() print(r) # more logic ahead... The problem here is as I loop sum() on a list of such size it takes approx. 1.3 s for each result. enter image description here Is there any efficient way to reduce the 1.3s wait per call? I also tried creating a kind of cache dictionary, but the deep_dive_logic() function runs in a multi-process environment hence there is need to cache this dictionary, either redis or a json.dump not a choice because of the size of dictionary mounts to around 236MB and adds up as overhead in inter-process communication if not cached. sums_dict = {0: sum(ten_million)} even_difference = (ten_million[1] - ten_million[0]) for i in range(len(ten_million) - 1): sums_dict[i+1] = sums_dict[i] - (even_difference * (i+1)) I need help with either caching of 10Million dictionary or an alternate formula to return the result without using sum() or any out-of-box solution.
1
# Users who requested the code (so far)
$9
Suggested price from buyers at which they’d buy
Potential revenues from requesters (actual revenues can be significantly higher)
Time of last request:on Feb 22, 2018
Tags:python, numpy
If you own such codes, please set up an offer and then invite users to it by clicking on «Invite to offer».
If you’re also be willing to buy those codes then click on «I’d also buy it» (no commitment to buy, but please only click when you’re serious about it).
Tell people about Sellcodes and get 20-40% on every resulting sale!