Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
itertools vs purity
#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


Messages In This Thread
itertools vs purity - by sparkz_alot - Oct-14-2017, 01:57 PM
RE: itertools vs purity - by buran - Oct-14-2017, 02:18 PM
RE: itertools vs purity - by sparkz_alot - Oct-14-2017, 09:10 PM
RE: itertools vs purity - by buran - Oct-14-2017, 09:14 PM
RE: itertools vs purity - by sparkz_alot - Oct-14-2017, 09:27 PM
RE: itertools vs purity - by buran - Oct-14-2017, 09:29 PM
RE: itertools vs purity - by sparkz_alot - Oct-15-2017, 12:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  itertools and amazing speed Pedroski55 8 2,354 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,648 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,389 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,191 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,970 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 7,180 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  can itertools compact a list removing all of some value? Skaperen 6 3,384 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,916 Jun-10-2019, 02:41 AM
Last Post: Larz60+
  ImportError: No module named jaraco.itertools in Python manhnt 0 3,090 Nov-08-2018, 11:41 AM
Last Post: manhnt
  Need help with itertools.islice() Charles1 2 2,966 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