Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
itertools vs purity
#1
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  Think ?) 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
Reply
#2
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
Reply
#3
(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
Reply
#4
(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).
Reply
#5
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
Reply
#6
(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 ...:-)
Reply
#7
lol, for me that was about 60 years ago, so it's possible.  Think
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  itertools and amazing speed Pedroski55 8 2,044 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,397 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,237 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,086 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,836 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,706 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  can itertools compact a list removing all of some value? Skaperen 6 3,160 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,808 Jun-10-2019, 02:41 AM
Last Post: Larz60+
  ImportError: No module named jaraco.itertools in Python manhnt 0 2,978 Nov-08-2018, 11:41 AM
Last Post: manhnt
  Need help with itertools.islice() Charles1 2 2,831 Sep-19-2018, 10:32 AM
Last Post: Charles1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020