Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

Ruby 2.5 Performance Boosts: String Interpolation 72% เร็วขึ้น, Enumerable Methods 50% เร็วขึ้น

Ruby 2.5 (release 25 ธันวาคม) มี optimizations ใหม่:

  • String interpolation ~72% เร็วขึ้น (large strings)
  • String#prepend ~42% เร็วขึ้น (single argument)
  • Enumerable#sort_by, #min_by, #max_by ~50% เร็วขึ้น

String Interpolation Performance

Benchmark (benchmark/ips):

Benchmark.ips do |x|
  x.report "Large string interpolation" do |t|
    a = "Hellooooooooooooooooooooooooooooooooooooooooooooooooooo"
    b = "Wooooooooooooooooooooooooooooooooooooooooooooooooooorld"
    t.times { "#{a}, #{b}!" }
  end
  x.report "Small string interpolation" do |t|
    a = "Hello"; b = "World"
    t.times { "#{a}, #{b}!" }
  end
  x.compare!
end

ผลลัพธ์:

Ruby 2.4.1:  Large: 1,711,633 i/s (1.89x slower than small)
Ruby 2.5:    Large: 2,555,782 i/s (1.22x slower than small)

Large strings เร็วขึ้นชัดเจน!

String#prepend Performance

Ruby 2.4.1: 3.428M i/s
Ruby 2.5:   4.638M i/s  (~42% faster)

Enumerable Performance Improvements

Optimization: ใช้ OPTIMIZED_CMP() แทน <=> method dispatching สำหรับ Fixnum/Float/String

Ruby 2.4.2 → Ruby 2.5:
sort_by:  2.395k → 5.914k i/s (~50%)
min_by:   8.244k → 15.668k i/s (~50%)
max_by:   8.053k → 15.544k i/s (~50%)

Range#min & Range#max

Ruby 2.4.2 → Ruby 2.5:
Range#min: 7.976M → 13.154M i/s (~65%)
Range#max: 7.996M → 13.021M i/s (~63%)

Improved String#scan

String pattern ~50% faster, Regex pattern ~10% faster

Ruby 2.4.2 → Ruby 2.5:
String pattern: 1.367M → 3.944M i/s
Regex pattern:  1.228M → 1.696M i/s

Summary

Ruby 2.5 optimizations: string interpolation, prepend, Enumerable (sort_by/min_by/max_by), Range min/max, String#scan