Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BDonnot opened this issue Jan 6, 2025 · 0 comments
Open

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

BDonnot opened this issue Jan 6, 2025 · 0 comments

Comments

@BDonnot
Copy link
Contributor

BDonnot commented Jan 6, 2025

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.

BDonnot added a commit that referenced this issue Jan 8, 2025
Signed-off-by: DONNOT Benjamin <[email protected]>
@BDonnot BDonnot mentioned this issue Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant