sisl.selector module¶
Sub-package to easily make algorithmic decisions based on different routines
Several functions exists here to most efficiently choose the most performant routine.
The Selector
will run through the different routines and decide, based on
all the calls which is the best one.
Basically the Selector
will only be a powerful tool if a given routine is
called numerous times.
The following example will show how the TimeSelector
may be used
to automatically call the fastest of 3 routines:
>>> def func1():
... print('Func - 1')
>>> def func2():
... print('Func - 2')
... time.sleep(1)
>>> def func3():
... print('Func - 3')
... time.sleep(1)
>>> selector = TimeSelector([func1, func2, func3])
>>> selector()
Func - 1
>>> selector()
Func - 2
>>> selector()
Func - 3
>>> selector() # will now only call the fastest of the 3
Func - 1
In certain cases one may wish to limit the search for a selected routine by only searching until the performance of the next called routine drops. This is called an ordered selector because it tries them in order, and once one is slower than the former tested ones, it will not test any further. For the above same functions we may do:
>>> selector = TimeSelector([func1, func2, func3], ordered=True)
>>> selector()
Func - 1
>>> selector()
Func - 2
>>> selector()
Func - 1
>>> selector()
Func - 1
-
class
sisl.selector.
Selector
(routines=None, ordered=False)[source]¶ Bases:
object
Base class for implementing a selector of class routines
This class should contain a list of routines and may then be used to always return the best performant routine.
This is done on a per-class basis where this class should initially determine which routine is the best performing one and then always return that one.
Attributes
routines (list of func) this is a list of functions that will be selected from. ordered (bool) If False a simple selection of the most performant one will be chosen. If True, it will check the routines in order and once one of the routines is less performant it will choose from the setof runned routines. Methods
__call__
(*args, **kwargs)Call the function that optimizes the run-time the most append
(routine)Prepends a new routine to the selector next
()Choose the next routine that requires performance analysis prepend
(routine)Prepends a new routine to the selector reset
()Reset the performance table to redo the performance checks select_best
([routine])Update the best
routine, if applicablestart
()Start the performance profiler stop
(start)Stop the performance profiler -
append
(routine)[source]¶ Prepends a new routine to the selector
Parameters: routine : func
the new routine to be tested in the selector
-
best
¶
-
next
()[source]¶ Choose the next routine that requires performance analysis
Returns: int, func :
a tuple with the
int
specifying the routine index.func
is the routine that is to be runned.
-
ordered
¶
-
performances
¶
-
prepend
(routine)[source]¶ Prepends a new routine to the selector
Parameters: routine : func
the new routine to be tested in the selector
-
routines
¶
-
select_best
(routine=None)[source]¶ Update the
best
routine, if applicableUpdate the selector to choose the best method. If not all routines have been carried through, then no best routine will be selected (unless
self.ordered
is True).By passing a routine as an argument that given routine will by default be the chosen best algorithm.
Parameters: routine : func or str
If
None
is passed (the default) it will select the best default routine based on the stored performances. If, however, not all performance values has been created no routine will be selected.If passing a
func
that function will be chosen as the best method
-
start
()[source]¶ Start the performance profiler
This routine should return an initial state value. The difference between
stop() - start()
should yield a performance identifier which may be used to control the used algorithm.A large performance identifier results in the use of the routine.
-
stop
(start)[source]¶ Stop the performance profiler
This routine should return an initial state value. The difference between
stop() - start()
should yield a performance identifier which may be used to control the used algorithm.A large performance identifier results in the use of the routine.
Parameters: start : float
the output of the
start()
routine to convert to actual performance identifier
-
-
class
sisl.selector.
TimeSelector
(routines=None, ordered=False)[source]¶ Bases:
sisl.selector.Selector
Routine performance selector based on timings for the routines
Attributes
best
ordered
performances
routines
Methods
__call__
(*args, **kwargs)Call the function that optimizes the run-time the most append
(routine)Prepends a new routine to the selector next
()Choose the next routine that requires performance analysis prepend
(routine)Prepends a new routine to the selector reset
()Reset the performance table to redo the performance checks select_best
([routine])Update the best
routine, if applicablestart
()Start the timing routine stop
(start)Stop the timing routine