mirror of
https://github.com/iritmaximus/tira.git
synced 2026-10-12 19:13:47 +03:00
45 lines
1014 B
Python
45 lines
1014 B
Python
import time
|
|
import random
|
|
|
|
|
|
def timespent(listLen):
|
|
"""
|
|
Creates list and calculates the time needed to make it
|
|
|
|
Args:
|
|
:param listLen: length of the tested list
|
|
Returns:
|
|
:param result: the time it took to make the list
|
|
"""
|
|
testList = []
|
|
for x in range(listLen):
|
|
testList.append(random.randint(0, 1))
|
|
|
|
startTimeO2 = time.time()
|
|
laskuri = 0
|
|
for x in range(listLen - 1):
|
|
for y in range(x + 1, listLen - 1):
|
|
if testList[x] == 0 and testList[y] == 1:
|
|
laskuri += 1
|
|
endTimeO2 = time.time()
|
|
|
|
startTimeO = time.time()
|
|
laskuri = 0
|
|
nollat = 0
|
|
for x in range(listLen - 1):
|
|
if testList[x] == 0:
|
|
nollat += 1
|
|
else:
|
|
laskuri += nollat
|
|
endTimeO = time.time()
|
|
|
|
timeToO2 = startTimeO2 - endTimeO2
|
|
timeToO = startTimeO - endTimeO
|
|
return timeToO2, timeToO
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(timespent(10**2))
|
|
print(timespent(10**4))
|
|
print(timespent(10**5))
|