API#
- ocalc.analyze_source(source, language=None)[source]#
Estimate worst-case time and space from a source snippet.
The reported class is the worst function in the snippet. Loops, recursion, and calls such as matrix products and sorts set that class.
- Parameters:
- Returns:
The detected language, the worst-case classes, and one row per function.
- Return type:
- class ocalc.SourceAnalysis(language: 'str', time: 'str', space: 'str', detail: 'str', functions: 'tuple[FunctionEstimate, ...]' = ())[source]#
- ocalc.measure(func, *sample_args, budget=None, min_n=None, max_n=None, measures=None, repeats=None, file=None, qualname=None, protocol=None, previous_s=None, **sample_kwargs)[source]#
Measure a function and return its complexity report.
- Parameters:
func – The callable to measure.
sample_args – Example positional arguments. The first collection grows with
n.budget (float | None) – Seconds to spend measuring. The default is 5.
min_n (int | None) – Smallest input size.
max_n (int | None) – Largest input size.
measures (int | None) – How many sizes to try.
repeats (int | None) – Timed calls at each size.
file (str | None) – Source file, when the callable should be re-imported for the timed run.
qualname (str | None) – Qualname used to re-import
file.protocol (Protocol | None) – Stored sizes and budget from an earlier comment.
previous_s (float | None) – Worst time from that earlier comment.
sample_kwargs – Example keyword arguments.
- Returns:
The comment, the size table, and the fitted time and space classes.
- Return type:
- ocalc.annotate_file(path, qualname, *sample_args, budget=None, min_n=None, max_n=None, measures=None, repeats=None, **sample_kwargs)[source]#
Benchmark a function in a file and write the comment above it.
A later call reuses the protocol stored in the existing comment and records the previous worst time.
- Parameters:
path – Python file that defines the function.
qualname (str) – Function qualname, such as
two_sumorBox.run.sample_args – Example positional arguments.
budget (float | None) – Seconds to spend measuring.
min_n (int | None) – Smallest input size.
max_n (int | None) – Largest input size.
measures (int | None) – How many sizes to try.
repeats (int | None) – Timed calls at each size.
sample_kwargs – Example keyword arguments.
- Returns:
The report that was written into the file.
- Return type:
- class ocalc.Report(comment: 'str', points: 'list[Point]', time: 'FitResult', space: 'FitResult', worst_s: 'float', median_s: 'float', worst_n: 'int | None', protocol: 'Protocol')[source]#