This is the main module of PySpeedIT. It is a combination of Benchmark-IT, Profile-IT, Line-Memory-Profile-IT, Disassemble-IT.
Define one or more separate modules with test functions.
Python-Example
# file: usage_example.py
from operator import itemgetter
from random import shuffle
def _outer_helper(y_):
return y_[1]
def example_pep265(data_):
shuffle(data_)
result = sorted(data_.items(), key=itemgetter(1))
del result
def example_formal_func_outer(data_):
shuffle(data_)
result = sorted(data_.items(), key=_outer_helper)
del result
# for Benchmark-IT subcode_blocks
def example_multiple_subcode_blocks():
# ::SPEEDIT:: data
data = dict(zip(range(1000), range(1000)))
# **SPEEDIT**
shuffle(data)
# ::SPEEDIT:: sorted
result = sorted(data.items(), key=itemgetter(1))
# **SPEEDIT**
del result
def memory_example():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
del a
Define an other module to run Speed-IT.
Python-Example
# file: run_speed_it __usage_example.py
# Import abspath
from os.path import abspath as path_abspath
# Import speed_it
from PySpeedIT.speed_it import speed_it
# define any needed extra variables to use as arguments
data = dict(zip(range(1000), range(1000)))
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
def main():
# defining the: modules_func_tuple mapping
modules__func_tuples = (
# TUPLE format:
# [module_path_str, (
# (name_str, function_name_str, list_of_positional_arguments, dictionary_of_keyword_arguments)
# )]
[path_abspath('usage_example.py'), (
('sorting: pep265', 'example_pep265', [data], {}),
('sorting: formal_func_outer', 'example_formal_func_outer', [data], {}),
('multiple_subcode_blocks', 'example_multiple_subcode_blocks', [], {}),
('memory_example', 'memory_example', [], {}),
)],
# any other module: a similar list
)
Note
Do not import the module with the functions to speed-it: the module is internally loaded from file
For more examples see any files in the PySpeedIT source SOURCE/Examples
Function which are used for speed_it may not have a return statement because they may run in a loop
Python-Example
WRONG return statement
# helper function is ok to have return statement
def recip_square(i_):
return 1.0 / (i_ ** 2)
# function to be used by speed_it can not have a return statement
def approx_pi(n_=100000):
val = 0.
for k_ in range(1, n_ + 1):
val += recip_square(k_)
return (6 * val) ** 0.5
If a function defines a default argument it still needs an argument passed on in the modules__func_tuples.
Python-Example
# function to be used by speed_it
def approx_pi(n_=100000):
val = 0.
for k_ in range(1, n_ + 1):
val += recip_square(k_)
return (6 * val) ** 0.5
WRONG: modules__func_tuples missing argument for : n_
modules__func_tuples = (
[path_abspath('calculate_pi.py'), (
('calculate pi', 'approx_pi', [], {}),
)],
)
OK: modules__func_tuples with argument for : n_
modules__func_tuples = (
[path_abspath('calculate_pi.py'), (
('calculate pi', 'approx_pi', [], {'n_': 100000}),
)],
)
Writes the results per defined module to html files overwriting them if they existed.
Parameters: |
|
---|