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 :: extract DATE from pandas 
Python :: custom attribute selenium 
Python :: forgot django admin password 
Python :: how to make a game in python 
Python :: online python 
Python :: parentheses in python 
Python :: new line in python 
Python :: python split by first match 
Python :: how to get a random number in python 
Python :: install virtual environments_brew 
Python :: countplot for different classes in a column 
Python :: make a condition statement on column pandas 
Python :: append write python 
Python :: list element swapping python 
Python :: in python 
Python :: python dict remove duplicates where name are not the same 
Python :: python discord 
Python :: Convert column as array to column as string before saving to csv 
Python :: code to take the picture 
Python :: password guessing game python 
Python :: glob python 
Python :: how to swap two variables without using third variable and default python functionality 
Python :: find element in list that matches a condition 
Python :: py -m pip 
Python :: sorting values in dictionary in python 
Python :: find number of unique keys in the dictionary 
Python :: Scrapping tables in an HTML file with BeautifulSoup 
Python :: numpy fill with 0 
Python :: how to change character in string python 
Python :: python abc 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =