Skip to content

Add a way to retrieve the computation time of the powerflow #4

@BDonnot

Description

@BDonnot

It is not documented (my bad...) but the Backend has an attribute called comp_time that can be filled with the actual time spent in the powerflow (this can be used to make benchmark between different backends easier).

Adding a first version of it in the package would not be difficult, it would require to change 2 functions:

    def reset(self,
              path : Union[os.PathLike, str],
              grid_filename : Optional[Union[os.PathLike, str]]=None) -> None:
        logger.info("Reset backend")
        self.load_grid(path, filename=grid_filename)
        self.comp_time = 0.  # added line

and

    def runpf(self, is_dc: bool = False) -> Tuple[bool, Union[Exception, None]]:
        logger.info(f"Running {'DC' if is_dc else 'AC'} powerflow")

        start_time = time.perf_counter()  # changed

        if self._check_isolated_and_disconnected_injections and self._native_backend.check_isolated_and_disconnected_injections():
            converged = False
        else:
            beg_ = time.perf_counter()   # added
            results = self._native_backend.run_pf(is_dc, self._lf_parameters)
            end_ = time.perf_counter()  # added
            self.comp_time += end_ - beg_  # added
            converged = self._is_converged(results[0])

        end_time = time.perf_counter()  # changed
        elapsed_time = (end_time - start_time) * 1000
        logger.info(f"Powerflow ran in {elapsed_time:.2f} ms")

        return converged, None if converged else DivergingPowerflow()

Also as a reference, when benchmarking times in python it's best to use time.perf_counter() rather than time.time()

I can of course do a PR for this if you prefer :-)

Indeed, perf_counter() is guaranteed to be monotonic (always increase) as per the doc https://docs.python.org/3/library/time.html#time.perf_counter

But time.time() can sometimes go backward (https://docs.python.org/3/library/time.html#time.time)

While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions