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 get_list() by 39% in posthog/settings/utils.py #2

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 May 16, 2024

📄 get_list() in posthog/settings/utils.py

📈 Performance improved by 39% (0.39x faster)

⏱️ Runtime went down from 201.59μs to 144.75μs

Explanation and details

The existing Python code is efficient, but you can make minimal changes by avoiding the strip() method if the text does not contain any leading or trailing spaces. Here you go.

This change only speeds things up if you know that most of your inputs will not have spaces around the commas. If the common case is that there are spaces around the commas, then the existing function is pretty optimal.

Correctness verification

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

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 19 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
import pytest  # used for our unit tests
from posthog.settings.utils import get_list

# unit tests

def test_basic_single_item():
    # Single item input
    assert get_list("apple") == ['apple']

def test_basic_multiple_items():
    # Multiple items input
    assert get_list("apple,banana,cherry") == ['apple', 'banana', 'cherry']

def test_whitespace_leading_trailing():
    # Leading and trailing whitespace
    assert get_list(" apple , banana , cherry ") == ['apple', 'banana', 'cherry']

def test_whitespace_internal():
    # Internal whitespace
    assert get_list("  apple  ,  banana  ,  cherry  ") == ['apple', 'banana', 'cherry']

def test_empty_string():
    # Empty string input
    assert get_list("") == []

def test_none_input():
    # None input
    assert get_list(None) == []

def test_multiple_consecutive_commas():
    # Multiple consecutive commas
    assert get_list("apple,,banana") == ['apple', '', 'banana']

def test_leading_comma():
    # Leading comma
    assert get_list(",apple,banana") == ['', 'apple', 'banana']

def test_trailing_comma():
    # Trailing comma
    assert get_list("apple,banana,") == ['apple', 'banana', '']

def test_only_commas():
    # Only commas
    assert get_list(",,,") == ['', '', '', '']

def test_special_characters():
    # Special characters in items
    assert get_list("apple, banana, cherry!, @fruit, #hashtag") == ['apple', 'banana', 'cherry!', '@fruit', '#hashtag']

def test_whitespace_characters():
    # Whitespace characters
    assert get_list("apple, \tbanana, \ncherry") == ['apple', 'banana', 'cherry']

def test_large_number_of_items():
    # Large number of items
    input_text = ",".join(["item" + str(i) for i in range(1000)])
    expected_output = ["item" + str(i) for i in range(1000)]
    assert get_list(input_text) == expected_output

def test_large_string_with_whitespace():
    # Large string with whitespace
    input_text = ",".join([" item " + str(i) + " " for i in range(1000)])
    expected_output = ["item" + str(i) for i in range(1000)]
    assert get_list(input_text) == expected_output

def test_very_long_single_item():
    # Very long single item
    input_text = "a" * 10000
    expected_output = ["a" * 10000]
    assert get_list(input_text) == expected_output

def test_very_long_string_with_multiple_items():
    # Very long string with multiple items
    input_text = ",".join(["a" * 100 for _ in range(1000)])
    expected_output = ["a" * 100 for _ in range(1000)]
    assert get_list(input_text) == expected_output

def test_mixed_content_numbers_strings():
    # Mixed content with numbers and strings
    assert get_list("apple, 123, banana, 456") == ['apple', '123', 'banana', '456']

def test_mixed_content_special_characters():
    # Mixed content with special characters
    assert get_list("apple, @123, banana, #456") == ['apple', '@123', 'banana', '#456']

def test_integer_input():
    # Integer input should raise TypeError
    with pytest.raises(TypeError):
        get_list(123)

def test_list_input():
    # List input should raise TypeError
    with pytest.raises(TypeError):
        get_list(["apple", "banana"])

The existing Python code is efficient, but you can make minimal changes by avoiding the strip() method if the text does not contain any leading or trailing spaces. Here you go.



This change only speeds things up if you know that most of your inputs will not have spaces around the commas. If the common case is that there are spaces around the commas, then the existing function is pretty optimal.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label May 16, 2024
@codeflash-ai codeflash-ai bot requested a review from aphexcx May 16, 2024 03:21
aphexcx pushed a commit that referenced this pull request Jun 25, 2024
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