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 :: python fibonacci 
Python :: python unlist flatten nested lists 
Python :: how to download the captions of a youtube video 
Python :: python remove multiple characters from string 
Python :: move one column value down by one column in pandas 
Python :: remove spaces from string python 
Python :: block window if another window is open tkinter 
Python :: python float to 2 decimals 
Python :: pywhatkit 
Python :: concatenate directories python 
Python :: python render_template 
Python :: normal distribution in python 
Python :: int to list python 
Python :: cannot safely cast non-equivalent float64 to int64 
Python :: accessing index of dataframe python 
Python :: remove a file or dir in linux or mac or ubuntu 
Python :: figsize param in pandas plot 
Python :: pillow rgb to grayscale 
Python :: python for loop counter 
Python :: pandas filter length of string 
Python :: django logout user 
Python :: logistic regression algorithm in python 
Python :: isistance exmaple 
Python :: pi python 
Python :: get sum in range 
Python :: get time format python2 hours minutes seconds milliseconds 
Python :: create new dataframe with columns from another dataframe pandas 
Python :: creating a virtual environment with django on windows 
Python :: python tkinter change color of main window 
Python :: python dictionary to array 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =