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 function get_full_name by 11% in docs_src/python_types/tutorial001.py #1

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

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Nov 22, 2024

📄 get_full_name() in docs_src/python_types/tutorial001.py

📈 Performance improved by 11% (0.11x faster)

⏱️ Runtime went down from 45.3 milliseconds to 40.9 milliseconds (best of 51 runs)

Explanation and details

Here's a more efficient version of the get_full_name function. By using f-strings for string concatenation and calling the title() method only once per string, we can slightly optimize the function.

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 1065 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
import pytest  # used for our unit tests
from docs_src.python_types.tutorial001 import get_full_name

# unit tests

def test_standard_inputs():
    # Typical first and last names
    codeflash_output = get_full_name("john", "doe")
    codeflash_output = get_full_name("jane", "smith")
    # Outputs were verified to be equal to the original implementation

def test_already_capitalized_inputs():
    # First and last names already in title case
    codeflash_output = get_full_name("John", "Doe")
    codeflash_output = get_full_name("Jane", "Smith")
    # Outputs were verified to be equal to the original implementation

def test_mixed_case_inputs():
    # First and last names with mixed casing
    codeflash_output = get_full_name("jOhN", "dOe")
    codeflash_output = get_full_name("JaNe", "sMiTh")
    # Outputs were verified to be equal to the original implementation

def test_empty_strings():
    # One or both inputs are empty
    codeflash_output = get_full_name("", "doe")
    codeflash_output = get_full_name("john", "")
    codeflash_output = get_full_name("", "")
    # Outputs were verified to be equal to the original implementation

def test_single_character_names():
    # First and last names consisting of a single character
    codeflash_output = get_full_name("j", "d")
    codeflash_output = get_full_name("a", "b")
    # Outputs were verified to be equal to the original implementation

def test_multiple_words_in_names():
    # First and/or last names containing multiple words
    codeflash_output = get_full_name("john", "van doe")
    codeflash_output = get_full_name("mary ann", "smith")
    # Outputs were verified to be equal to the original implementation

def test_names_with_leading_or_trailing_whitespace():
    # First and/or last names with leading or trailing spaces
    codeflash_output = get_full_name(" john", "doe ")
    codeflash_output = get_full_name("  john", "doe  ")
    # Outputs were verified to be equal to the original implementation

def test_non_ascii_characters():
    # Names with accented or non-ASCII characters
    codeflash_output = get_full_name("josé", "niño")
    codeflash_output = get_full_name("éclair", "crème brûlée")
    # Outputs were verified to be equal to the original implementation

def test_numeric_and_special_characters_in_names():
    # Names containing numbers or special characters
    codeflash_output = get_full_name("john123", "doe!")
    codeflash_output = get_full_name("mary-jane", "o'connor")
    # Outputs were verified to be equal to the original implementation

def test_very_long_names():
    # Extremely long first and/or last names
    codeflash_output = get_full_name("a" * 1000, "b" * 1000)
    codeflash_output = get_full_name("longfirstname", "longlastname" * 100)
    # Outputs were verified to be equal to the original implementation

def test_names_with_punctuation():
    # Names containing punctuation
    codeflash_output = get_full_name("john.", "doe,")
    codeflash_output = get_full_name("mary-ann", "o'connor")
    # Outputs were verified to be equal to the original implementation

def test_performance_and_scalability():
    # Assess function performance with large data samples
    codeflash_output = get_full_name("a" * 1000000, "b" * 1000000)
    # Outputs were verified to be equal to the original implementation

def test_names_with_special_unicode_characters():
    # First and/or last names containing special Unicode characters
    codeflash_output = get_full_name("😀", "😎")
    codeflash_output = get_full_name("こんにちは", "世界")
    codeflash_output = get_full_name("مرحبا", "عالم")
    # Outputs were verified to be equal to the original implementation

def test_names_with_control_characters():
    # First and/or last names containing control characters (e.g., newline, tab)
    codeflash_output = get_full_name("john\n", "doe")
    codeflash_output = get_full_name("john", "doe\t")
    codeflash_output = get_full_name("john\n", "doe\t")
    # Outputs were verified to be equal to the original implementation

def test_names_with_only_whitespace():
    # First and/or last names consisting entirely of whitespace
    codeflash_output = get_full_name("   ", "doe")
    codeflash_output = get_full_name("john", "   ")
    codeflash_output = get_full_name("   ", "   ")
    # Outputs were verified to be equal to the original implementation

def test_names_with_mixed_language_characters():
    # First and/or last names containing characters from different languages
    codeflash_output = get_full_name("john", "山田")
    codeflash_output = get_full_name("Мария", "smith")
    codeflash_output = get_full_name("李", "johnson")
    # Outputs were verified to be equal to the original implementation

def test_names_with_leading_or_trailing_special_characters():
    # First and/or last names with special characters at the beginning or end
    codeflash_output = get_full_name("#john", "doe")
    codeflash_output = get_full_name("john", "doe!")
    codeflash_output = get_full_name("@john", "@doe")
    # Outputs were verified to be equal to the original implementation

def test_names_with_embedded_null_characters():
    # First and/or last names containing null characters (\0)
    codeflash_output = get_full_name("john\0doe", "smith")
    codeflash_output = get_full_name("john", "smith\0doe")
    # Outputs were verified to be equal to the original implementation

def test_names_with_repeated_characters():
    # First and/or last names with repeated characters
    codeflash_output = get_full_name("aaaa", "bbbb")
    codeflash_output = get_full_name("john", "doe"*100)
    # Outputs were verified to be equal to the original implementation
import pytest  # used for our unit tests
from docs_src.python_types.tutorial001 import get_full_name

# unit tests

def test_standard_cases():
    # Both names in lowercase
    codeflash_output = get_full_name("john", "doe")
    codeflash_output = get_full_name("jane", "smith")
    
    # Both names in uppercase
    codeflash_output = get_full_name("JOHN", "DOE")
    codeflash_output = get_full_name("JANE", "SMITH")
    
    # Mixed case names
    codeflash_output = get_full_name("jOhN", "dOe")
    codeflash_output = get_full_name("JaNe", "sMiTh")
    # Outputs were verified to be equal to the original implementation

def test_edge_cases():
    # Empty strings
    codeflash_output = get_full_name("", "doe")
    codeflash_output = get_full_name("john", "")
    codeflash_output = get_full_name("", "")
    
    # Names with multiple words
    codeflash_output = get_full_name("john paul", "jones")
    codeflash_output = get_full_name("mary jane", "watson parker")
    
    # Names with hyphens or apostrophes
    codeflash_output = get_full_name("jean-luc", "picard")
    codeflash_output = get_full_name("o'connor", "mc'gregor")
    # Outputs were verified to be equal to the original implementation

def test_non_string_inputs():
    # Numeric inputs
    with pytest.raises(AttributeError):
        get_full_name(123, "doe")
    with pytest.raises(AttributeError):
        get_full_name("john", 456)
    with pytest.raises(AttributeError):
        get_full_name(123, 456)
    
    # None as input
    with pytest.raises(AttributeError):
        get_full_name(None, "doe")
    with pytest.raises(AttributeError):
        get_full_name("john", None)
    with pytest.raises(AttributeError):
        get_full_name(None, None)
    # Outputs were verified to be equal to the original implementation

def test_special_characters():
    # Names with special characters
    codeflash_output = get_full_name("john@", "doe#")
    codeflash_output = get_full_name("jane$", "smith%")
    # Outputs were verified to be equal to the original implementation

def test_large_scale_cases():
    # Very long names
    first_name = "a" * 1000
    last_name = "b" * 1000
    expected_full_name = "A" + "a" * 999 + " " + "B" + "b" * 999
    codeflash_output = get_full_name(first_name, last_name)
    # Outputs were verified to be equal to the original implementation

def test_performance_and_scalability():
    # Large number of calls
    for _ in range(1000):
        codeflash_output = get_full_name("john", "doe")
    # Outputs were verified to be equal to the original implementation

🔘 (none found) − ⏪ Replay Tests

Here's a more efficient version of the `get_full_name` function. By using f-strings for string concatenation and calling the `title()` method only once per string, we can slightly optimize the function.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Nov 22, 2024
@codeflash-ai codeflash-ai bot requested a review from Saga4 November 22, 2024 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ codeflash Optimization PR opened by Codeflash AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants