Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python time using timeit module

from timeit import default_timer as timer

start = timer()

print(23*2.3)

end = timer()
print(end - start)
Comment

how to use timeit in python 3

import timeit
import_module = "import random"
testcode = ''' 
def test(): 
    return random.randint(10, 100)
'''
print(timeit.repeat(stmt=testcode, setup=import_module))
Comment

python timeit

import timeit

def list_comprehension():
   """comprehension"""
   l = [i for i in range(10_000)]

def list_range():
   """list range"""
   l = list(range(10_000))

# number of executions to test
expSize = 1000

# time the functions 1000 times
time1 = timeit.timeit(list_comprehension, number=expSize)
time2 = timeit.timeit(list_range, number=expSize)
print("list_comprehension() execution result time", time1)
print("list_range() execution result time", time2)
print("list_comprehension() time divided by func2 time", time1/time2)
# list_comprehension() execution result time 0.14136912487447262
# list_range() execution result time 0.08224983396939933
# list_comprehension() time divided by func2 time 1.718777024243822
# list(range) executes faster than range in a list comprehension

# time the functions 1000 times and store the results in a list after each repeat test
time1 = timeit.repeat(list_comprehension, number=expSize, repeat=3)
time2 = timeit.repeat(list_range, number=expSize, repeat=3)
print("list_comprehension() execution result time", time1)
print("list_range() execution result time", time2)
# list_comprehension() execution result time [0.13349625002592802, 0.13272812496870756, 0.13246491691097617]
# list_range() execution result time [0.08029774995520711, 0.07969208294525743, 0.08046487509272993]

print("-".join(str(n) for n in range(100)))
# output 0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30...

range_str_joined_time = timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
print(range_str_joined_time)
# output 0.08738012495450675
Comment

PREVIOUS NEXT
Code Example
Python :: install models python 
Python :: datetime not defined python 
Python :: python confidence interval 
Python :: python check file format 
Python :: pytest skip 
Python :: how to read a file into array in python 
Python :: install a specific version of django 
Python :: open website python 
Python :: python create a list of alphabets 
Python :: how to get pc name with python 
Python :: how to loop in python 
Python :: roc curve python 
Python :: hbox(children=(floatprogress(value= 
Python :: console clear python 
Python :: knn sklearn 
Python :: how to change background color in python turtle 
Python :: python get current number of threads 
Python :: genspider scrapy 
Python :: python datetime add minutes 
Python :: python array delete last column 
Python :: exponentiation is the raising of one number to the power of another. this operation is performed using two asterisks **. 
Python :: np array value count 
Python :: python string list to list 
Python :: python print code 
Python :: install library from python code 
Python :: python console animation 
Python :: 2 - 20 python 
Python :: convert a dictionary into dataframe python 
Python :: load saved model 
Python :: ver todas linhas dataframe pandas 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =