Class: PrettyIRB::Benchmarker

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_irb/benchmarker.rb

Class Method Summary collapse

Class Method Details

.benchmark(code, iterations = 1000) ⇒ Object

Benchmark a code snippet



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pretty_irb/benchmarker.rb', line 6

def self.benchmark(code, iterations = 1000)
  begin
    time = Benchmark.measure do
      iterations.times do
        eval(code)
      end
    end

    total_ms = time.real * 1000
    per_iteration_us = (total_ms * 1000) / iterations

    format_benchmark_result(code, iterations, total_ms, per_iteration_us)
  rescue => e
    "❌ Benchmark failed: #{e.message}"
  end
end

.compare(code1, code2, iterations = 1000) ⇒ Object

Compare two code snippets



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pretty_irb/benchmarker.rb', line 24

def self.compare(code1, code2, iterations = 1000)
  begin
    time1 = Benchmark.measure do
      iterations.times { eval(code1) }
    end

    time2 = Benchmark.measure do
      iterations.times { eval(code2) }
    end

    format_comparison(code1, code2, time1, time2, iterations)
  rescue => e
    "❌ Comparison failed: #{e.message}"
  end
end

.profile_memory(code, iterations = 100) ⇒ Object

Profile memory



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pretty_irb/benchmarker.rb', line 41

def self.profile_memory(code, iterations = 100)
  begin
    gc_stat_before = GC.stat
    
    iterations.times { eval(code) }
    
    gc_stat_after = GC.stat

    format_memory_profile(gc_stat_before, gc_stat_after, iterations)
  rescue => e
    "❌ Memory profile failed: #{e.message}"
  end
end