Skip to content

Latest commit

 

History

History
224 lines (174 loc) · 6.86 KB

File metadata and controls

224 lines (174 loc) · 6.86 KB

Python Code Complexity & Performance Analyzer

A comprehensive GUI application for analyzing Python code performance, complexity, and benchmarking built with wxPython.

Features

🎯 Core Analysis Capabilities

  • Performance Analysis: Speed, memory usage, CPU utilization
  • Complexity Analysis: Time and space complexity detection
  • Benchmarking: Real performance metrics with statistical analysis
  • Static Code Analysis: Function/class structure analysis

📊 Analysis Types

Performance Metrics

  • Execution time profiling (function-level and line-level)
  • Memory usage tracking with peak and average measurements
  • CPU utilization monitoring during execution
  • Memory leak detection
  • Performance stability analysis

Complexity Analysis

  • Time complexity estimation (O(1) to O(n!))
  • Space complexity analysis
  • Cyclomatic complexity calculation
  • Loop nesting depth analysis
  • Recursive function detection
  • Algorithm pattern identification

Benchmarking

  • Multiple iteration timing with statistical analysis
  • Memory pressure testing
  • Cold vs warm start comparisons
  • Concurrent execution testing
  • Performance consistency scoring

🖥️ User Interface

  • File Selection: Support for single files, directories, and modules
  • Analysis Options: Configurable analysis depth and parameters
  • Results Visualization: Charts, graphs, and detailed reports
  • Export Functionality: Save results in multiple formats

Installation

Prerequisites

  • Python 3.7 or higher
  • pip package manager

Install Dependencies

pip install -r requirements.txt

Alternative Installation (individual packages)

pip install wxPython>=4.2.0 matplotlib>=3.5.0 numpy>=1.21.0 psutil>=5.9.0

Usage

Starting the Application

python main.py

Basic Workflow

  1. Select Files

    • Use the "File Selection" tab
    • Choose single files, directories, or modules
    • Set file filters (default: *.py)
    • Add files to the analysis list
  2. Configure Analysis

    • Go to "Analysis Options" tab
    • Select analysis types:
      • ✅ Performance Analysis
      • ✅ Complexity Analysis
      • ✅ Benchmarking
      • ✅ Static Code Analysis
    • Configure options:
      • Memory profiling level (Basic/Detailed/Line-by-line)
      • Time profiling level (Function/Line/Both)
      • Benchmark iterations and warmup runs
  3. Run Analysis

    • Click "Start Analysis" button
    • Monitor progress in status bar
    • Analysis runs in background thread
  4. View Results

    • Summary: High-level overview and recommendations
    • Performance: Detailed performance metrics and profiling
    • Complexity: Function complexity analysis and hotspots
    • Benchmarks: Statistical analysis and performance stability
    • Charts: Visual representations of performance data
    • Raw Data: Export data in JSON, pretty print, or CSV formats

Analysis Details

Performance Analysis

  • Execution Time: Average, min, max, standard deviation
  • Memory Usage: Peak memory, memory timeline, leak detection
  • CPU Usage: Average and peak CPU utilization
  • Function Profiling: Per-function timing and call counts

Complexity Analysis

  • Time Complexity: Automatically detects common complexity patterns
  • Space Complexity: Analyzes memory allocation patterns
  • Cyclomatic Complexity: Measures code complexity based on control flow
  • Hotspot Detection: Identifies functions needing optimization
  • Algorithm Patterns: Recognizes sorting, searching, recursion patterns

Benchmarking

  • Statistical Analysis: Mean, median, standard deviation, percentiles
  • Performance Stability: Consistency scoring and trend analysis
  • Comparative Analysis: Cold vs warm start, memory pressure testing
  • Concurrent Execution: Multi-threading performance impact

File Structure

Python Complexity/
├── main.py                    # Main GUI application
├── performance_analyzer.py    # Performance analysis module
├── complexity_analyzer.py     # Complexity analysis module
├── benchmark_runner.py        # Benchmarking module
├── results_viewer.py          # Results display module
├── requirements.txt           # Dependencies
└── README.md                 # This file

Example Analysis Output

Performance Summary

Files Analyzed: 3
File: example_script.py
----------------------------------------
  Average Execution Time: 0.002341s
  Peak Memory Usage: 15.23 MB
  Time Complexity: O(n²)
  Space Complexity: O(n)
  Functions Analyzed: 5
  Complexity Hotspots: 1
  Performance Stability: good

Complexity Hotspots

  • Functions with high complexity (O(n²) or worse)
  • High cyclomatic complexity (>10)
  • Deep loop nesting (>2 levels)
  • Inefficient algorithm patterns

Recommendations

  • Optimize O(n²) algorithms to O(n log n) where possible
  • Break down complex functions (cyclomatic complexity >15)
  • Consider memoization for recursive functions
  • Use more efficient data structures

Supported File Types

  • Python Scripts (.py): Full analysis support
  • Python Modules: Package and module analysis
  • Directories: Recursive analysis of all Python files

Limitations

  • Only analyzes Python code
  • Dynamic analysis requires executable code
  • Some complex imports may not execute properly
  • Memory analysis has overhead on very large datasets
  • Concurrent analysis limited to avoid system overload

Troubleshooting

Common Issues

  1. "Module not found" errors

    • Ensure all dependencies are installed
    • Check that target files have proper imports
  2. "Execution failed" errors

    • Code may have runtime dependencies not available
    • Check that code can run independently
  3. High memory usage during analysis

    • Reduce benchmark iterations
    • Analyze files individually instead of in batches
  4. GUI not responding

    • Analysis runs in background threads
    • Large files may take time to process

Performance Tips

  • Start with smaller iteration counts for initial analysis
  • Use "Basic" memory profiling for faster results
  • Analyze individual files before bulk directory analysis
  • Close other applications to ensure accurate memory/CPU measurements

Contributing

This is a standalone analysis tool. To extend functionality:

  1. Add new analysis methods to respective modules
  2. Update the GUI panels in results_viewer.py
  3. Extend the main application in main.py

License

This project is provided as-is for educational and development purposes.

Support

For issues or questions:

  1. Check the troubleshooting section above
  2. Verify all dependencies are correctly installed
  3. Test with simple Python files first
  4. Check console output for detailed error messages

Note: This tool is designed for code analysis and optimization. Results should be interpreted in context of your specific use case and requirements.