From 39297f9957de97a2ed2c06bc5bdd0657b697f249 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Sat, 8 Feb 2025 14:29:49 -0800 Subject: [PATCH] Speed up function `replace_placeholders` by 10% (#338) **Summary of Optimizations:** 1. **Redundant Calls**: Captured `get_date_input()` in the variable `date_input` to reduce redundant calls. 2. **Reuse Computations**: Calculated `name_snake_case` and `name_kebab_case` once and reused them instead of recalculating multiple times. 3. **Better Loop**: Used a tuple of tuples for `placeholders`, iterating through once and applying replacements in sequence. These changes improve the efficiency of the `replace_placeholders` function by reducing redundant calculations and I/O operations, resulting in better runtime performance. Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com> --- rendercv/data/models/computers.py | 40 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/rendercv/data/models/computers.py b/rendercv/data/models/computers.py index 88226026..5d3b2ef0 100644 --- a/rendercv/data/models/computers.py +++ b/rendercv/data/models/computers.py @@ -105,26 +105,30 @@ def replace_placeholders(value: str) -> str: full_month_names = locale["full_names_of_months"] short_month_names = locale["abbreviations_for_months"] - month = get_date_input().month - year = str(get_date_input().year) + date_input = get_date_input() + month = date_input.month + year = str(date_input.year) - placeholders = { - "NAME_IN_SNAKE_CASE": name.replace(" ", "_"), - "NAME_IN_LOWER_SNAKE_CASE": name.replace(" ", "_").lower(), - "NAME_IN_UPPER_SNAKE_CASE": name.replace(" ", "_").upper(), - "NAME_IN_KEBAB_CASE": name.replace(" ", "-"), - "NAME_IN_LOWER_KEBAB_CASE": name.replace(" ", "-").lower(), - "NAME_IN_UPPER_KEBAB_CASE": name.replace(" ", "-").upper(), - "FULL_MONTH_NAME": full_month_names[month - 1], - "MONTH_ABBREVIATION": short_month_names[month - 1], - "MONTH_IN_TWO_DIGITS": f"{month:02d}", - "YEAR_IN_TWO_DIGITS": str(year[-2:]), - "NAME": name, - "YEAR": str(year), - "MONTH": str(month), - } + name_snake_case = name.replace(" ", "_") + name_kebab_case = name.replace(" ", "-") - for placeholder, placeholder_value in placeholders.items(): + placeholders = ( + ("NAME_IN_SNAKE_CASE", name_snake_case), + ("NAME_IN_LOWER_SNAKE_CASE", name_snake_case.lower()), + ("NAME_IN_UPPER_SNAKE_CASE", name_snake_case.upper()), + ("NAME_IN_KEBAB_CASE", name_kebab_case), + ("NAME_IN_LOWER_KEBAB_CASE", name_kebab_case.lower()), + ("NAME_IN_UPPER_KEBAB_CASE", name_kebab_case.upper()), + ("FULL_MONTH_NAME", full_month_names[month - 1]), + ("MONTH_ABBREVIATION", short_month_names[month - 1]), + ("MONTH_IN_TWO_DIGITS", f"{month:02d}"), + ("YEAR_IN_TWO_DIGITS", year[-2:]), + ("NAME", name), + ("YEAR", year), + ("MONTH", str(month)), + ) + + for placeholder, placeholder_value in placeholders: value = value.replace(placeholder, placeholder_value) return value