Skip to main content
Learn how to optimize your sandbox performance to reduce execution time and improve efficiency.

Resource Sizing

Right-size your sandbox resources for optimal performance:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

# ✅ Good: Match resources to workload
# Lightweight workloads
sandbox = Sandbox.create(
    template="code-interpreter",
    vcpu=1,
    memory_mb=512,
    disk_gb=5
)

# Data analysis workloads
sandbox = Sandbox.create(
    template="code-interpreter",
    vcpu=4,
    memory_mb=4096,
    disk_gb=20
)

# ML training workloads
sandbox = Sandbox.create(
    template="code-interpreter",
    vcpu=16,
    memory_mb=16384,
    disk_gb=100
)

Reuse Sandboxes

Reuse sandboxes for multiple operations to avoid startup overhead:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

# ✅ Good: Reuse sandbox for multiple operations
with Sandbox.create(template="code-interpreter") as sandbox:
    # Install packages once
    sandbox.commands.run("pip install pandas numpy --quiet")
    
    # Run multiple operations
    result1 = sandbox.run_code("import pandas as pd; print('Operation 1')")
    result2 = sandbox.run_code("import numpy as np; print('Operation 2')")
    result3 = sandbox.run_code("print('Operation 3')")

# ❌ Bad: Create new sandbox for each operation
# sandbox1 = Sandbox.create(...)
# sandbox1.run_code("operation1")
# sandbox2 = Sandbox.create(...)  # Unnecessary overhead
# sandbox2.run_code("operation2")

Parallel Execution

Run independent operations in parallel:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox
import concurrent.futures

def run_parallel_operations(operations: list):
    """Run multiple operations in parallel"""
    def run_operation(op: dict):
        with Sandbox.create(template="code-interpreter") as sandbox:
            return sandbox.run_code(op["code"])
    
    # Run in parallel
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        results = list(executor.map(run_operation, operations))
    
    return results

# Example
operations = [
    {"code": "import time; time.sleep(2); print('Task 1')"},
    {"code": "import time; time.sleep(2); print('Task 2')"},
    {"code": "import time; time.sleep(2); print('Task 3')"}
]

results = run_parallel_operations(operations)
# All tasks complete in ~2 seconds instead of ~6 seconds

Optimize Code Execution

Write efficient code for better performance:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox

# ✅ Good: Efficient code
code = """
import pandas as pd
import numpy as np

# Use vectorized operations
df = pd.DataFrame({'a': range(1000000)})
df['b'] = df['a'] * 2  # Fast: vectorized

# Avoid loops when possible
result = df['a'].sum()  # Fast: vectorized
# result = sum(df['a'])  # Slower: Python loop
"""

# ❌ Bad: Inefficient code
# code = """
# result = 0
# for i in range(1000000):
#     result += i  # Slow: Python loop
# """

Monitor Performance

Monitor sandbox performance to identify bottlenecks:
  • Python
  • JavaScript/TypeScript
from hopx_ai import Sandbox
import time

def monitor_performance(sandbox: Sandbox, code: str):
    """Monitor code execution performance"""
    start_time = time.time()
    
    result = sandbox.run_code(code)
    
    execution_time = time.time() - start_time
    
    # Get metrics
    metrics = sandbox.get_metrics()
    
    return {
        "execution_time": execution_time,
        "cpu_percent": metrics.cpu_percent,
        "memory_used_mb": metrics.memory_used_mb,
        "memory_total_mb": metrics.memory_total_mb
    }

with Sandbox.create(template="code-interpreter") as sandbox:
    perf = monitor_performance(sandbox, """
import pandas as pd
df = pd.DataFrame({'a': range(100000)})
result = df['a'].sum()
    """)
    
    print(f"Execution time: {perf['execution_time']:.2f}s")
    print(f"CPU usage: {perf['cpu_percent']}%")
    print(f"Memory usage: {perf['memory_used_mb']}/{perf['memory_total_mb']} MB")

Best Practices Summary

Match resources to your workload:
  • Lightweight: 1 vCPU, 512MB RAM
  • Data analysis: 4 vCPU, 4GB RAM
  • ML training: 16 vCPU, 16GB RAM
Reuse sandboxes for multiple operations to avoid startup overhead.
Run independent operations in parallel to reduce total execution time.
Use vectorized operations (NumPy, Pandas) instead of Python loops.
Monitor CPU, memory, and execution time to identify bottlenecks.
Don’t use more resources than needed - it increases costs without improving performance.
Don’t create a new sandbox for each operation - reuse when possible.

Next Steps