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

⚡️ Speed up percentage() by 40% in posthog/templatetags/posthog_filters.py #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Jun 28, 2024

  1. ⚡️ Speed up percentage() by 40%

    #### Why these changes?
    We initially refactored the code to opt for a more modern and efficient way of string formatting using f-strings.
    Then, we combined this with lazy evaluation to remove unnecessary computations.
    In the final stages, we micro-optimized by introducing constant pre-calculation and single-line conditionals.
    
    #### Correctness
    The better micro-optimized code maintains the same functionality as the original code.
    - Checks if the value is None and returns "-" if it is.
    - Multiplies the value by 100 and formats it as a percentage with the specified number of decimal places if the value is not None.
    Thus, it produces the same outputs for the same inputs and has the same side effects.
    
    #### How is this faster?
    The use of f-strings improves string formatting speed.
    Lazy evaluation immediately returns for None values, avoiding unnecessary calculations.
    Removing repeated computations (e.g., pre-calculating constant multipliers) reduces the computational overhead.
    Single-line conditionals make the execution path shorter and more efficient.
    codeflash-ai[bot] authored Jun 28, 2024
    Configuration menu
    Copy the full SHA
    8ad7c84 View commit details
    Browse the repository at this point in the history