In [1]:
%timeit [i for i in range(100)]
3.29 µs ± 41.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [2]:
%%timeit -o
import random
nums = random.choices(range(100, 900), k=50)
_min = nums[0]
for n in nums:
    if n < _min:
        _min = n
#print(_min)
21.7 µs ± 329 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Out[2]:
<TimeitResult : 21.7 µs ± 329 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)>
In [21]:
result = _
In [22]:
print(result)
22.6 µs ± 504 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [23]:
help(result)
Help on TimeitResult in module IPython.core.magics.execution object:

class TimeitResult(builtins.object)
 |  Object returned by the timeit magic with info about the run.
 |  
 |  Contains the following attributes :
 |  
 |  loops: (int) number of loops done per measurement
 |  repeat: (int) number of times the measurement has been repeated
 |  best: (float) best execution time / number
 |  all_runs: (list of float) execution time of each run (in s)
 |  compile_time: (float) time of statement compilation (s)
 |  
 |  Methods defined here:
 |  
 |  __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __str__(self)
 |      Return str(self).
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  average
 |  
 |  stdev

In [4]:
eval("print('Hello World')")
Hello World
In [8]:
import timeit
import random

def find_min(n_len):
    nums = random.choices(range(100, 1_000_000), k=n_len)
    _min = nums[0]
    for n in nums:
        if n < _min:
            _min = n
            
#t = timeit.Timer("find_min()", setup="from __main__ import find_min")
results = []
for i in range(100, 3000, 300):
    t = timeit.Timer("find_minimum(i)", globals={"find_minimum":find_min,
                                                "i": i})
    results.append(t.timeit(10))
print(results)
[0.000562836998142302, 0.0018033020023722202, 0.003130425000563264, 0.00492637199931778, 0.00628084599156864, 0.006916017009643838, 0.00805098400451243, 0.009583841005223803, 0.010575145992334, 0.011551852992852218]
In [42]:
print(results,len(results))
[0.00047253997763618827, 0.0010359379812143743, 0.0013636790099553764, 0.0018427949980832636, 0.002031921991147101, 0.00328956003068015, 0.003793541982304305, 0.0033902659779414535, 0.003684249008074403, 0.004256132000591606, 0.004866461968049407, 0.004951882001478225, 0.005840329977218062, 0.005644396005664021, 0.006137057032901794, 0.006686966982670128, 0.0070683350204490125, 0.008146886015310884, 0.00801611301722005, 0.00904042599722743, 0.009881165984552354, 0.008971638977527618, 0.00951933499891311, 0.010136898956261575, 0.010306383017450571, 0.010728584020398557, 0.011289582995232195, 0.011674117995426059, 0.013450602011289448] 29
In [45]:
import random
nums = []
%timeit nums.extend(random.choices(range(100, 900), k=50))
_min = nums[0]
for n in nums:
    if n < _min:
        _min = n
#print(_min)
22 µs ± 715 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [46]:
a_list = []
a_list.append([1,2,3,4])
In [47]:
a_list
Out[47]:
[[1, 2, 3, 4]]
In [48]:
import time
help(time)
Help on built-in module time:

NAME
    time - This module provides various functions to manipulate time values.

DESCRIPTION
    There are two standard representations of time.  One is the number
    of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
    or a floating point number (to represent fractions of seconds).
    The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
    The actual value can be retrieved by calling gmtime(0).
    
    The other representation is a tuple of 9 integers giving local time.
    The tuple items are:
      year (including century, e.g. 1998)
      month (1-12)
      day (1-31)
      hours (0-23)
      minutes (0-59)
      seconds (0-59)
      weekday (0-6, Monday is 0)
      Julian day (day in the year, 1-366)
      DST (Daylight Savings Time) flag (-1, 0 or 1)
    If the DST flag is 0, the time is given in the regular time zone;
    if it is 1, the time is given in the DST time zone;
    if it is -1, mktime() should guess based on the date and time.
    
    Variables:
    
    timezone -- difference in seconds between UTC and local standard time
    altzone -- difference in  seconds between UTC and local DST time
    daylight -- whether local time should reflect DST
    tzname -- tuple of (standard time zone name, DST time zone name)
    
    Functions:
    
    time() -- return current time in seconds since the Epoch as a float
    clock() -- return CPU time since process start as a float
    sleep() -- delay for a number of seconds given as a float
    gmtime() -- convert seconds since Epoch to UTC tuple
    localtime() -- convert seconds since Epoch to local time tuple
    asctime() -- convert time tuple to string
    ctime() -- convert time in seconds to string
    mktime() -- convert local time tuple to seconds since Epoch
    strftime() -- convert time tuple to string according to format specification
    strptime() -- parse string to time tuple according to format specification
    tzset() -- change the local timezone

CLASSES
    builtins.tuple(builtins.object)
        struct_time
    
    class struct_time(builtins.tuple)
     |  The time value as returned by gmtime(), localtime(), and strptime(), and
     |  accepted by asctime(), mktime() and strftime().  May be considered as a
     |  sequence of 9 integers.
     |  
     |  Note that several fields' values are not the same as those defined by
     |  the C language standard for struct tm.  For example, the value of the
     |  field tm_year is the actual year, not year - 1900.  See individual
     |  fields' descriptions for details.
     |  
     |  Method resolution order:
     |      struct_time
     |      builtins.tuple
     |      builtins.object
     |  
     |  Methods defined here:
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |  
     |  __reduce__(...)
     |      helper for pickle
     |  
     |  __repr__(self, /)
     |      Return repr(self).
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  tm_gmtoff
     |      offset from UTC in seconds
     |  
     |  tm_hour
     |      hours, range [0, 23]
     |  
     |  tm_isdst
     |      1 if summer time is in effect, 0 if not, and -1 if unknown
     |  
     |  tm_mday
     |      day of month, range [1, 31]
     |  
     |  tm_min
     |      minutes, range [0, 59]
     |  
     |  tm_mon
     |      month of year, range [1, 12]
     |  
     |  tm_sec
     |      seconds, range [0, 61])
     |  
     |  tm_wday
     |      day of week, range [0, 6], Monday is 0
     |  
     |  tm_yday
     |      day of year, range [1, 366]
     |  
     |  tm_year
     |      year, for example, 1993
     |  
     |  tm_zone
     |      abbreviation of timezone name
     |  
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |  
     |  n_fields = 11
     |  
     |  n_sequence_fields = 9
     |  
     |  n_unnamed_fields = 0
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from builtins.tuple:
     |  
     |  __add__(self, value, /)
     |      Return self+value.
     |  
     |  __contains__(self, key, /)
     |      Return key in self.
     |  
     |  __eq__(self, value, /)
     |      Return self==value.
     |  
     |  __ge__(self, value, /)
     |      Return self>=value.
     |  
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |  
     |  __getitem__(self, key, /)
     |      Return self[key].
     |  
     |  __getnewargs__(...)
     |  
     |  __gt__(self, value, /)
     |      Return self>value.
     |  
     |  __hash__(self, /)
     |      Return hash(self).
     |  
     |  __iter__(self, /)
     |      Implement iter(self).
     |  
     |  __le__(self, value, /)
     |      Return self<=value.
     |  
     |  __len__(self, /)
     |      Return len(self).
     |  
     |  __lt__(self, value, /)
     |      Return self<value.
     |  
     |  __mul__(self, value, /)
     |      Return self*value.n
     |  
     |  __ne__(self, value, /)
     |      Return self!=value.
     |  
     |  __rmul__(self, value, /)
     |      Return self*value.
     |  
     |  count(...)
     |      T.count(value) -> integer -- return number of occurrences of value
     |  
     |  index(...)
     |      T.index(value, [start, [stop]]) -> integer -- return first index of value.
     |      Raises ValueError if the value is not present.

FUNCTIONS
    asctime(...)
        asctime([tuple]) -> string
        
        Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
        When the time tuple is not present, current time as returned by localtime()
        is used.
    
    clock(...)
        clock() -> floating point number
        
        Return the CPU time or real time since the start of the process or since
        the first call to clock().  This has as much precision as the system
        records.
    
    clock_getres(...)
        clock_getres(clk_id) -> floating point number
        
        Return the resolution (precision) of the specified clock clk_id.
    
    clock_gettime(...)
        clock_gettime(clk_id) -> floating point number
        
        Return the time of the specified clock clk_id.
    
    clock_settime(...)
        clock_settime(clk_id, time)
        
        Set the time of the specified clock clk_id.
    
    ctime(...)
        ctime(seconds) -> string
        
        Convert a time in seconds since the Epoch to a string in local time.
        This is equivalent to asctime(localtime(seconds)). When the time tuple is
        not present, current time as returned by localtime() is used.
    
    get_clock_info(...)
        get_clock_info(name: str) -> dict
        
        Get information of the specified clock.
    
    gmtime(...)
        gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,
                               tm_sec, tm_wday, tm_yday, tm_isdst)
        
        Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
        GMT).  When 'seconds' is not passed in, convert the current time instead.
        
        If the platform supports the tm_gmtoff and tm_zone, they are available as
        attributes only.
    
    localtime(...)
        localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
                                  tm_sec,tm_wday,tm_yday,tm_isdst)
        
        Convert seconds since the Epoch to a time tuple expressing local time.
        When 'seconds' is not passed in, convert the current time instead.
    
    mktime(...)
        mktime(tuple) -> floating point number
        
        Convert a time tuple in local time to seconds since the Epoch.
        Note that mktime(gmtime(0)) will not generally return zero for most
        time zones; instead the returned value will either be equal to that
        of the timezone or altzone attributes on the time module.
    
    monotonic(...)
        monotonic() -> float
        
        Monotonic clock, cannot go backward.
    
    perf_counter(...)
        perf_counter() -> float
        
        Performance counter for benchmarking.
    
    process_time(...)
        process_time() -> float
        
        Process time for profiling: sum of the kernel and user-space CPU time.
    
    sleep(...)
        sleep(seconds)
        
        Delay execution for a given number of seconds.  The argument may be
        a floating point number for subsecond precision.
    
    strftime(...)
        strftime(format[, tuple]) -> string
        
        Convert a time tuple to a string according to a format specification.
        See the library reference manual for formatting codes. When the time tuple
        is not present, current time as returned by localtime() is used.
        
        Commonly used format codes:
        
        %Y  Year with century as a decimal number.
        %m  Month as a decimal number [01,12].
        %d  Day of the month as a decimal number [01,31].
        %H  Hour (24-hour clock) as a decimal number [00,23].
        %M  Minute as a decimal number [00,59].
        %S  Second as a decimal number [00,61].
        %z  Time zone offset from UTC.
        %a  Locale's abbreviated weekday name.
        %A  Locale's full weekday name.
        %b  Locale's abbreviated month name.
        %B  Locale's full month name.
        %c  Locale's appropriate date and time representation.
        %I  Hour (12-hour clock) as a decimal number [01,12].
        %p  Locale's equivalent of either AM or PM.
        
        Other codes may be available on your platform.  See documentation for
        the C library strftime function.
    
    strptime(...)
        strptime(string, format) -> struct_time
        
        Parse a string to a time tuple according to a format specification.
        See the library reference manual for formatting codes (same as
        strftime()).
        
        Commonly used format codes:
        
        %Y  Year with century as a decimal number.
        %m  Month as a decimal number [01,12].
        %d  Day of the month as a decimal number [01,31].
        %H  Hour (24-hour clock) as a decimal number [00,23].
        %M  Minute as a decimal number [00,59].
        %S  Second as a decimal number [00,61].
        %z  Time zone offset from UTC.
        %a  Locale's abbreviated weekday name.
        %A  Locale's full weekday name.
        %b  Locale's abbreviated month name.
        %B  Locale's full month name.
        %c  Locale's appropriate date and time representation.
        %I  Hour (12-hour clock) as a decimal number [01,12].
        %p  Locale's equivalent of either AM or PM.
        
        Other codes may be available on your platform.  See documentation for
        the C library strftime function.
    
    time(...)
        time() -> floating point number
        
        Return the current time in seconds since the Epoch.
        Fractions of a second may be present if the system clock provides them.
    
    tzset(...)
        tzset()
        
        Initialize, or reinitialize, the local timezone to the value stored in
        os.environ['TZ']. The TZ environment variable should be specified in
        standard Unix timezone format as documented in the tzset man page
        (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently
        fall back to UTC. If the TZ environment variable is not set, the local
        timezone is set to the systems best guess of wallclock time.
        Changing the TZ environment variable without calling tzset *may* change
        the local timezone used by methods such as localtime, but this behaviour
        should not be relied on.

DATA
    CLOCK_MONOTONIC = 6
    CLOCK_MONOTONIC_RAW = 4
    CLOCK_PROCESS_CPUTIME_ID = 12
    CLOCK_REALTIME = 0
    CLOCK_THREAD_CPUTIME_ID = 16
    altzone = 14400
    daylight = 1
    timezone = 18000
    tzname = ('EST', 'EDT')

FILE
    (built-in)


In [49]:
time.clock()
Out[49]:
364.260628
In [50]:
s = time.clock()
for i in range(100000):
    pass
e = time.clock()
print(e - s)
0.004916999999977634
In [30]:
import time
In [31]:
time.perf_counter()
Out[31]:
424491.991751847
In [32]:
time.perf_counter() 
Out[32]:
424496.427418442
In [33]:
time.perf_counter() 
Out[33]:
424498.132330101
In [34]:
time.perf_counter() 
Out[34]:
424499.8277009
In [35]:
time.perf_counter() 
Out[35]:
424502.555954804
In [36]:
time.time()
Out[36]:
1594040990.9618812
In [38]:
s = time.perf_counter_ns() 
time.sleep(3)
e = time.perf_counter_ns()
print(e-s)
3004486436