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


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,105 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,432 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,258 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,111 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,866 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,796 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  can itertools compact a list removing all of some value? Skaperen 6 3,202 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,830 Jun-10-2019, 02:41 AM
Last Post: Larz60+
  ImportError: No module named jaraco.itertools in Python manhnt 0 3,001 Nov-08-2018, 11:41 AM
Last Post: manhnt
  Need help with itertools.islice() Charles1 2 2,852 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