Posts: 1,298
Threads: 38
Joined: Sep 2016
I haven't really used itertools so I thought I would try the 'accumulate' attribute against the equivalent math formula. Is it possible that the formula is truly that fast (faster than time itself  ?) Or is there a better way to time this.
import itertools
import time
a_list = []
for member in range(1, 1000001):
a_list.append(member)
list_length = len(a_list)
def accum(a_list):
acc = itertools.accumulate(a_list)
for n in acc:
total = n
print("Total from itertools = ", total)
return
def by_formula(list_length):
same_result = list_length * (list_length + 1) / 2
print("\nTotal from formula = ", int(same_result))
return
start_1 = time.time()
first = accum(a_list)
print('first took {0:0.7f} seconds'.format(time.time() - start_1))
start_2 = time.time()
second = by_formula(list_length)
print('second took {0:0.10f} seconds'.format(time.time() - start_2)) Output: Total from itertools = 500000500000
first took 0.0312371 seconds
Total from formula = 500000500000
second took 0.0000000000 seconds
Process finished with exit code 0
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 8,167
Threads: 160
Joined: Sep 2016
well, I don't think it is correct to compare these two. itertools.accumulate will iterate over len(a_lit) number of sums, and you a calculating just the last one.
it's correct to compare with sum
import itertools
import time
a_list = []
for member in range(1, 1000001):
a_list.append(member)
list_length = len(a_list)
def accum(a_list):
acc = itertools.accumulate(a_list)
for n in acc:
total = n
print("Total from itertools = ", total)
return
def by_formula(list_length):
same_result = list_length * (list_length + 1) / 2
print("\nTotal from formula = ", int(same_result))
return
def by_sum(a_list):
print("\nTotal from sum = ", sum(a_list))
return
start_1 = time.time()
first = accum(a_list)
print('first took {0:0.7f} seconds'.format(time.time() - start_1))
start_2 = time.time()
second = by_formula(list_length)
print('second took {0:0.10f} seconds'.format(time.time() - start_2))
start_3 = time.time()
third = by_sum(a_list)
print('third took {0:0.10f} seconds'.format(time.time() - start_3)) Output: Total from itertools = 500000500000
first took 0.0474508 seconds
Total from formula = 500000500000
second took 0.0000219345 seconds
Total from sum = 500000500000
third took 0.0085046291 seconds
however, you can use formula, e.g. sum of arithmetic progression or geometric progression only in some cases, depending of the sequence
e.g. if the list has random numbers, formula will not help
Posts: 1,298
Threads: 38
Joined: Sep 2016
Oct-14-2017, 09:10 PM
(This post was last modified: Oct-14-2017, 09:10 PM by sparkz_alot.)
(Oct-14-2017, 02:18 PM)buran Wrote: if the list has random numbers, formula will not help
Makes sense. The truth of the matter is I saw the formula flash by on a TV show and I wondered if it was real or just some gibberish they put on to make people think it was a real formula. When I compared the result to that of 'accumulate' I was surprised to see it actually worked. Then I wondered how fast each one was, but all I got from 'by_formula' was zeros, even when I changed the list size to 10,000,000 and the accuracy to 100 decimal places. So I'm glad to see you got an actual 'time'. I didn't even think of 'sum', so thanks for pointing that out.
My apologies to the author of the formula, they said your name but I thought that too was fake, so I forgot it as soon as they said it.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 8,167
Threads: 160
Joined: Sep 2016
Oct-14-2017, 09:14 PM
(This post was last modified: Oct-14-2017, 09:15 PM by buran.)
(Oct-14-2017, 09:10 PM)sparkz_alot Wrote: The truth of the matter is I saw the formula flash by on a TV show
well, that's the formula for sum of arithmetic progression, it is school math... According to wikipedia ( https://en.wikipedia.org/wiki/Arithmetic_progression)
In 499 AD Aryabhata, a prominent mathematician- astronomer from the classical age of Indian mathematics and Indian astronomy, gave this method in the Aryabhatiya (section 2.18).
Posts: 1,298
Threads: 38
Joined: Sep 2016
You contain a wealth of information. The author thanks you for giving him the credit he is due, I on the other hand, still think it looks fake and forgot it (again) as soon as I finished reading your post.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 8,167
Threads: 160
Joined: Sep 2016
(Oct-14-2017, 09:27 PM)sparkz_alot Wrote: I on the other hand, still think it looks fake Didn't you study it in school? I think in my case it was 6 or 7th grade.. :-) along with formula for sum of geometric progression ...:-)
Posts: 1,298
Threads: 38
Joined: Sep 2016
lol, for me that was about 60 years ago, so it's possible.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
|