Python Timeit() koos näidetega

Mis on Python Timeit()?

Python timeit () on meetod Python teek, et mõõta antud koodilõigu täitmisaega. The Python teek käivitab koodilause 1 miljon korda ja annab minimaalse aja, mis on võetud antud koodilõikude komplektist. Python timeit() on kasulik meetod, mis aitab kontrollida koodi jõudlust.

Süntaks

timeit.timeit(stmt, setup,timer, number)

parameetrid

  • stmt: see võtab koodi, mille täitmise aega soovite mõõta. Vaikeväärtus on "pass".
  • seade: sellel on seadistuse üksikasjad, mis tuleb käivitada enne stmt. Vaikeväärtus on "pass".
  • taimer: This will have the timer value, timeit() already has a default value set, and we can ignore it.
  • number: The stmt will execute as per the number is given here. The default value is 1000000.

Funktsiooni timeit() kasutamiseks peame importima mooduli, nagu allpool näidatud:

import timeit

Esimene näide

Here is a simple example of timeit() function

Code Näiteks 1

# testing timeit()
import timeit
print(timeit.timeit('output = 10*5'))

Väljund:

0.06127880399999999

We have seen a simple example that gives us the execution time of the simple code statement output = 10*5, and the time is taken to execute it is 0.06127880399999999.

Timing Multiple lines in python code

There are two ways to execute multiple lines of code in timeit.timeit(): by separating the lines with semicolons or enclosing the code as a string with triple quotes.

Here are examples that show the working of it.

Näide 1: semikooloni kasutamine

import timeit
print("The time taken is ",timeit.timeit(stmt='a=10;b=10;sum=a+b'))

Väljund:

The time taken is  0.137031482

Example 2: Using triple quotes

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

Väljund:

C:\pythontest>python testtimeit.py
The time taken is  0.182619178

timeit – meetodid

Here, are 2 important timeit methods

timeit.default_timer() : This will return the default time when executed.

timeit.repeat(stmt, setup, timer, repeat, number) : sama, mis timeit() , kuid korduse korral nimetatakse timeit() kordamiste arvu, mis on antud.

Program Example 1

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

Väljund:

0.46715912400000004

Näiteks 2

default_timer() Näide

# testing timeit()
 
import timeit
import random
 
def test(): 
    return random.randint(10, 100)
 
starttime = timeit.default_timer()
print("The start time is :",starttime)
test()
print("The time difference is :", timeit.default_timer() - starttime)

Väljund:

The start time is : 0.220261875
The time difference is : 0.0004737320000000045

Näide 3: timeit.repeat()

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

Väljund:

 [0.43638873, 0.5040939680000001, 0.5069179909999999, 0.3943449330000002, 0.3546886979999999]

timeit.repeat() works similar to timeit.timeit() function, with the only difference it takes in the repeat argument and gives back the execution time in array format with values as per the repeat number.

Executing timing function timeit.timeit() inside command-line interface

Süntaks teie funktsiooni täitmiseks käsureal timeit() sees on järgmine:

python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [code statement ...]

Käsurea parameetrid:

  • -n N: the number of times you want the code to execute.
  • -r N: kordade arv, mitu korda soovite funktsiooni timeit() kordamise
  • -s S: sellel on seadistuse üksikasjad, mis käivitatakse enne koodi käivitamist.
  • -t: for this, you can make use of time.time()
  • -c: for this, you can make use of time.clock()
  • -h: for help
  • code statement: The code details.

Näide

C:\pythontest>python -m timeit -s 'text="hello world"'
20000000 loops, best of 5: 13.1 nsec per loop

Another way you can execute inside command line is as shown below:

Näide

>>> import timeit
>>> print("The time taken is ",timeit.timeit(stmt='a=10;b=10;sum=a+b'))
The time taken is  0.15048536300000137
>>>

Why is timeit() the best way to measure the execution time of Python code?

Siin on mõned põhjused, miks me arvame, et timeit() on parim viis täitmisaja mõõtmiseks.

  • It runs the code statement 1 million times that is the default value, and from that, it will return you the minimum time taken. You can also increase/decrease the 1 million by setting the argument number in time () function.
  • Testi sooritamise ajal on prügi kogumine aeg () funktsiooniga iga kord keelatud.
  • timeit() internally takes the accurate time as per your operating system being used. For example, it will use time.clock() for Windows operating system and time.time() for mac and Linux.

kokkuvõte

Timeit() kasutatakse antud väikese koodi täitmise aja saamiseks

Parameters used with timeit()

  • stmt: see võtab koodi, mille täitmise aega soovite mõõta
  • setup: sellel on seadistuse üksikasjad, mis tuleb käivitada enne stmt
  • timer: This will have the timer value, timeit() already has a default value set, and we can ignore it.
  • number: stmt käivitatakse vastavalt siin antud numbrile.

Võta see postitus kokku järgmiselt: