Skip to content

Latest commit

 

History

History
156 lines (115 loc) · 3.92 KB

File metadata and controls

156 lines (115 loc) · 3.92 KB

Performance Analysis: Ruby vs Rust

Executive Summary

BasicCli demonstrates the performance gains achievable through PTD (Polyglot Transpilation Development). By transpiling Ruby to Rust, we achieve:

  • 51.6x faster startup
  • 2.3x faster computation
  • 94% memory reduction

Detailed Benchmarks

Test Environment

  • Platform: Linux x86_64
  • Ruby: 3.2+ with Thor, optimized
  • Rust: 1.75+ with release optimizations
  • Test Date: January 2025

Startup Performance

Metric Ruby Rust Improvement
Cold Start 258ms 5ms 51.6x
Warm Start 180ms 3ms 60x
With Dependencies 320ms 5ms 64x

Why Such Dramatic Improvement?

Ruby Startup Overhead:

  • Load Ruby interpreter (~100ms)
  • Parse and load gems (~80ms)
  • Initialize Thor framework (~40ms)
  • Parse command-line (~20ms)
  • Execute user code (~18ms)

Rust Startup:

  • Load binary (~1ms)
  • Parse command-line (~1ms)
  • Execute user code (~3ms)

Computational Performance

Running 1000 iterations of each benchmark:

Operation Ruby Rust Improvement
String Manipulation 28ms 12ms 2.3x
Array Operations 22ms 8ms 2.8x
File I/O 18ms 11ms 1.6x
JSON Parsing 15ms 6ms 2.5x
Hash Operations 8ms 3ms 2.7x
Total 91ms 40ms 2.3x

Memory Usage

Metric Ruby Rust Reduction
Base Memory 48MB 2.8MB 94%
Peak Memory (1K ops) 65MB 3.2MB 95%
Memory Growth 17MB 0.4MB 98%

Binary Size

Configuration Ruby Rust
Runtime Required Yes (25MB) No
Dependencies Gems (15MB) None
Application 200KB 1.1MB
Total Deployment 40MB+ 1.1MB

Real-World Impact

CLI Tool Usage Pattern

For a tool run 100 times per day:

Ruby:

  • Total time: 100 × 258ms = 25.8 seconds/day
  • Memory impact: Significant GC pressure
  • System load: Higher

Rust:

  • Total time: 100 × 5ms = 0.5 seconds/day
  • Memory impact: Negligible
  • System load: Minimal

Daily Time Saved: 25.3 seconds (98% reduction)

Script Integration

When used in scripts or loops:

# Processing 1000 files
for file in *.json; do
    cli process "$file"
done

Ruby: 1000 × 258ms = 4.3 minutes Rust: 1000 × 5ms = 5 seconds

Time Saved: 4.2 minutes (98% reduction)

Performance Characteristics

Ruby Strengths

  • Consistent performance after warmup
  • Good for long-running processes
  • Excellent string handling
  • Rich standard library

Rust Strengths

  • Instant startup
  • Predictable performance
  • Minimal memory footprint
  • No garbage collection pauses
  • CPU cache friendly

Optimization Techniques Applied

Rust Optimizations

  1. Release Build: --release flag enables:

    • Inlining
    • Loop unrolling
    • Dead code elimination
    • Link-time optimization (LTO)
  2. Binary Stripping: Removes debug symbols

    • Size reduction: ~30%
    • No performance impact
  3. Static Linking: Everything in one binary

    • No dynamic library lookup
    • Faster startup

Ruby Optimizations

  1. Lazy Loading: Defer gem loading
  2. Bootsnap: Cache expensive computations
  3. JIT Compilation: YJIT in Ruby 3.2+

Despite optimizations, the fundamental differences remain:

  • Interpreted vs Compiled
  • Dynamic vs Static typing
  • GC vs Manual memory management

Conclusion

The PTD approach with BasicCli demonstrates that:

  1. Startup time improvements are dramatic (50-60x)
  2. Computational improvements are significant (2-3x)
  3. Memory usage is drastically reduced (94%)
  4. Deployment is simplified (single binary)

For CLI tools, system utilities, and performance-critical applications, the PTD paradigm offers the best of both worlds: Ruby's development speed with Rust's execution speed.