{"meta":{"title":"Refactoring for performance optimization","intro":"Copilot Chat can suggest ways to speed up slow-running code.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat Cookbook"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/refactor-code","title":"Refactor code"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/refactor-code/refactor-for-optimization","title":"Refactor for optimization"}],"documentType":"article"},"body":"# Refactoring for performance optimization\n\nCopilot Chat can suggest ways to speed up slow-running code.\n\nExisting code may function correctly but be inefficient, leading to performance bottlenecks. Examples of coding that can adversely impact performance include inefficient loops, unnecessary computations, and excessive memory allocation.\n\nCopilot Chat can identify performance hotspots and suggest refactoring strategies such as optimizing data structures (for example, using hashmaps instead of arrays), reducing redundant calculations, and improving algorithmic efficiency. It can also recommend caching mechanisms or parallelizing operations to improve performance.\n\n## Example scenario\n\nThe following Python code prints every 1000th prime number up to the 10,000th prime. It includes a timer, and prints how long the code takes to run as the last line of the output. The program takes several seconds to complete.\n\n```python id=slow-prime\nimport time\n\ndef is_prime(n):\n    if n <= 1:\n        return False\n    for i in range(2, n):\n        if n % i == 0:\n            return False\n    return True\n\ndef every_1000th_prime():\n    primes = []\n    num = 2\n    while len(primes) < 10000:\n        if is_prime(num):\n            primes.append(num)\n        num += 1\n    return [primes[i] for i in range(999, 10000, 1000)]\n\nif __name__ == \"__main__\":\n    start_time = time.time()\n    primes = every_1000th_prime()\n    for prime in primes:\n        print(prime)\n    end_time = time.time()\n    elapsed_time = end_time - start_time\n    print(f\"Time taken: {elapsed_time:.10f} seconds\")\n```\n\n## Example prompt\n\n```copilot copy prompt ref=slow-prime\nOptimize this code for performance\n```\n\n## Example response\n\nCopilot suggests using a sieve function to generate the prime numbers much more efficiently. If you run this program it will complete in a fraction of a second.\n\n> \\[!NOTE] Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.\n\n```python\nimport time\n\ndef sieve_of_eratosthenes(limit):\n    sieve = [True] * (limit + 1)\n    sieve[0] = sieve[1] = False\n    for start in range(2, int(limit**0.5) + 1):\n        if sieve[start]:\n            for multiple in range(start*start, limit + 1, start):\n                sieve[multiple] = False\n    return [num for num, is_prime in enumerate(sieve) if is_prime]\n\ndef every_1000th_prime():\n    limit = 105000  # Higher than needed to ensure 10,000 primes\n    primes = sieve_of_eratosthenes(limit)\n    return [primes[i] for i in range(999, 10000, 1000)]\n\nif __name__ == \"__main__\":\n    start_time = time.time()\n    primes = every_1000th_prime()\n    for prime in primes:\n        print(prime)\n    end_time = time.time()\n    elapsed_time = end_time - start_time\n    print(f\"Time taken: {elapsed_time:.10f} seconds\")\n```\n\n## Further reading\n\n* [Prompt engineering for GitHub Copilot Chat](/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [Best practices for using GitHub Copilot](/en/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}