Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
122-COM/slow_functions.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
38 lines (27 sloc)
759 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, timeit, time | |
def n_sum(sequence): | |
total = 0 | |
for i in range(len(sequence)): | |
total += sequence[i] | |
time.sleep(0.001) | |
return total | |
def n2_sum(sequence): | |
total = 0 | |
for i in range(len(sequence)): | |
counter = 0 | |
while counter < i: | |
counter += 1 | |
total += sequence[counter] | |
return total | |
if __name__ == '__main__': | |
n = 1 | |
for i in range(100): | |
n *= 2 | |
a = [ i for i in range(n) ] | |
nStart = timeit.default_timer() | |
n_sum( a ) | |
nTook = timeit.default_timer() - nStart | |
n2Start = timeit.default_timer() | |
n2_sum( a ) | |
n2Took = timeit.default_timer() - n2Start | |
print('%d\t%f\t%f' % (n,nTook,n2Took)) |