কম্পিউটার

বেঞ্চমার্কিং রুবি পরিশোধন

আপনি যদি পিছনের গল্পটি না জেনেই রুবি পরিমার্জনগুলির জন্য Google অনুসন্ধান করেন তবে আপনি এই ধারণাটি নিয়ে আসতে পারেন যে পরিমার্জনগুলি ধীর।

যেহেতু সেগুলি মূলত প্রস্তাবিত ছিল, পরিমার্জনগুলি ধীরগতির হত৷ তারা দোভাষীর পক্ষে পদ্ধতির সন্ধানের মতো জিনিসগুলিকে অপ্টিমাইজ করা অসম্ভব করে তুলত।

কিন্তু পরিমার্জনার প্রকৃত বাস্তবায়ন মূল প্রস্তাবের চেয়ে একটু বেশি সীমিত। তাই আমি ভেবেছিলাম যে আজ রুবিতে বিদ্যমান পরিমার্জনগুলির উপর মানদণ্ডের একটি সিরিজ চালানো আকর্ষণীয় হবে৷

TL;DR

পরিমার্জন ধীর হয় না. অথবা অন্তত তারা "স্বাভাবিক" পদ্ধতির চেয়ে ধীর বলে মনে হয় না।

ডামি লোড

আমরা বেঞ্চমার্কিং পদ্ধতি কল হতে যাচ্ছি. তাই আমাদের কয়েকটি পদ্ধতির প্রয়োজন হবে।

এখানে, আমি একটি নির্বোধ সামান্য পদ্ধতির দুটি সংস্করণ তৈরি করছি। একটি হল "স্বাভাবিক" এবং অন্যটি একটি পরিমার্জনার ভিতরে:

# As our "dummy load" we're going to create shrugs. 
# 1 shrug == "¯\_(ツ)_/¯"
# 2 shrugs == "¯\_(ツ)_/¯¯\_(ツ)_/¯"
# ...etc.

SHRUG = "¯\_(ツ)_/¯"

# We'll make a refinement that generates shrugs
module Shruggable
  refine Fixnum do
    def shrugs
      SHRUG * self
    end
  end
end

# ...and we'll make a normal method that also generates shrugs
def integer_to_shrugs(n)
  SHRUG * n
end

আমরা সরাসরি পরিশোধন ব্যবহার করতে পারি না। এটি একটি using এর মাধ্যমে সক্রিয় করতে হবে বিবৃতি তাই আমি দুটি ক্লাস তৈরি করব যা একইভাবে আচরণ করে। একটি পরিশোধন ব্যবহার করে, এবং অন্যটি করে না।

class TestUsing
  using Shruggable
  def noop
  end

  def shrug
    10.shrugs
  end
end

class TestWithoutUsing
  def noop
  end

  def shrug
    integer_to_shrugs(10)
  end
end

বেঞ্চমার্ক

আমি জানতে চেয়েছিলাম যে পরিমার্জন ব্যবহার করে বস্তুগুলিকে ইনস্ট্যান্টিয়েট করা, অথবা পরিমার্জনের মাধ্যমে যোগ করা পদ্ধতিগুলিকে কল করা কোন ধীর ছিল কিনা৷

OSX El Capitan-এ MRI 2.2.2 দিয়ে সমস্ত বেঞ্চমার্ক চালিত হয়েছিল।

বস্তু তৈরি

"ব্যবহার করা" কীওয়ার্ডটি কি ক্লাস শুরু করতে ধীর করে তোলে? না।

Benchmark.ips do |bm|
  bm.report("class initialization") { TestUsing.new }
  bm.report("class initialization WITH using") { TestWithoutUsing.new }
  bm.compare!
end

# Calculating -------------------------------------
# class initialization   142.929k i/100ms
# class initialization WITH using
#                        145.323k i/100ms
# -------------------------------------------------
# class initialization      5.564M (± 8.3%) i/s -     27.728M
# class initialization WITH using
#                           5.619M (± 7.4%) i/s -     28.047M
# Comparison:
# class initialization WITH using:  5618601.3 i/s
# class initialization:  5564116.5 i/s - 1.01x slower

পদ্ধতি কল

পরিমার্জনগুলি কি "স্বাভাবিক" পদ্ধতির সন্ধানের গতিকে প্রভাবিত করে? না।

Benchmark.ips do |bm|
  bm.report("run method") { TestUsing.new.noop }
  bm.report("run method in class WITH using") { TestWithoutUsing.new.noop }
  bm.compare!
end

# Calculating -------------------------------------
#           run method   141.905k i/100ms
# run method in class WITH using
#                        144.435k i/100ms
# -------------------------------------------------
#           run method      5.010M (± 6.4%) i/s -     24.975M
# run method in class WITH using
#                           5.086M (± 5.3%) i/s -     25.421M
# Comparison:
# run method in class WITH using:  5086262.3 i/s
#           run method:  5010273.6 i/s - 1.02x slower

একটি সমতুল্য "স্বাভাবিক" পদ্ধতি ব্যবহার করার চেয়ে একটি পরিশোধন থেকে একটি পদ্ধতি ব্যবহার করা কি ধীর? না।

Benchmark.ips do |bm|
  bm.report("shrug") { TestUsing.new.shrug }
  bm.report("shrug via refinement") { TestWithoutUsing.new.shrug }
  bm.compare!
end

# Calculating -------------------------------------
#                shrug    96.089k i/100ms
# shrug via refinement    95.559k i/100ms
# -------------------------------------------------
#                shrug      1.825M (± 9.3%) i/s -      9.128M
# shrug via refinement      1.929M (± 6.2%) i/s -      9.651M

# Comparison:
# shrug via refinement:  1928841.5 i/s
#                shrug:  1825069.4 i/s - 1.06x slower

ডেক স্ট্যাক করা

আমি কি আমার নিয়ন্ত্রণের চেয়ে ধীরগতির পরিমার্জন বেঞ্চমার্ক করতে কিছু করতে পারি? ¯\_(ツ)_/¯


# Does repeated evaluation of the `using` keyword affect performance. Only slightly. 
# This is an unfair test, but I really wanted to force refinements to be slower
# in SOME use case :)
Benchmark.ips do |bm|
  bm.report("inline shrug") { integer_to_shrugs(10) }
  bm.report("inline shrug via refinement") do
    using Shruggable
    10.shrugs
  end
  bm.compare!
end

# Calculating -------------------------------------
#         inline shrug   100.460k i/100ms
# inline shrug via refinement
#                         72.131k i/100ms
# -------------------------------------------------
#         inline shrug      2.507M (± 5.2%) i/s -     12.557M
# inline shrug via refinement
#                           1.498M (± 4.3%) i/s -      7.502M

# Comparison:
#         inline shrug:  2506663.9 i/s
# inline shrug via refinement:  1497747.6 i/s - 1.67x slower

সম্পূর্ণ কোড

আপনি যদি নিজে বেঞ্চমার্ক চালাতে চান, তাহলে কোডটি এখানে দেওয়া হল।

require 'benchmark/ips'


# As our "dummy load" we're going to create shrugs. 
# 1 shrug == "¯\_(ツ)_/¯"
# 2 shrugs == "¯\_(ツ)_/¯¯\_(ツ)_/¯"
# ...etc.

SHRUG = "¯\_(ツ)_/¯"

# We'll make a refinement that generates shrugs
module Shruggable
  refine Fixnum do
    def shrugs
      SHRUG * self
    end
  end
end

# ...and we'll make a normal method that also generates shrugs
def integer_to_shrugs(n)
  SHRUG * n
end

# Now we'll define two classes. The first uses refinments. The second doesn't. 
class TestUsing
  using Shruggable
  def noop
  end

  def shrug
    10.shrugs
  end
end

class TestWithoutUsing
  def noop
  end

  def shrug
    integer_to_shrugs(10)
  end
end

# Does the "using" keyword make a class slower to initialize? Nope. 
Benchmark.ips do |bm|
  bm.report("class initialization") { TestUsing.new }
  bm.report("class initialization WITH using") { TestWithoutUsing.new }
  bm.compare!
end

# Calculating -------------------------------------
# class initialization   142.929k i/100ms
# class initialization WITH using
#                        145.323k i/100ms
# -------------------------------------------------
# class initialization      5.564M (± 8.3%) i/s -     27.728M
# class initialization WITH using
#                           5.619M (± 7.4%) i/s -     28.047M
# Comparison:
# class initialization WITH using:  5618601.3 i/s
# class initialization:  5564116.5 i/s - 1.01x slower

# Do refinements affect "normal" method lookup speed? Nope. 
Benchmark.ips do |bm|
  bm.report("run method") { TestUsing.new.noop }
  bm.report("run method in class WITH using") { TestWithoutUsing.new.noop }
  bm.compare!
end

# Calculating -------------------------------------
#           run method   141.905k i/100ms
# run method in class WITH using
#                        144.435k i/100ms
# -------------------------------------------------
#           run method      5.010M (± 6.4%) i/s -     24.975M
# run method in class WITH using
#                           5.086M (± 5.3%) i/s -     25.421M
# Comparison:
# run method in class WITH using:  5086262.3 i/s
#           run method:  5010273.6 i/s - 1.02x slower


# Is using a method from a refinement slower than using an equivalent "normal" method? Nope. 
Benchmark.ips do |bm|
  bm.report("shrug") { TestUsing.new.shrug }
  bm.report("shrug via refinement") { TestWithoutUsing.new.shrug }
  bm.compare!
end

# Calculating -------------------------------------
#                shrug    96.089k i/100ms
# shrug via refinement    95.559k i/100ms
# -------------------------------------------------
#                shrug      1.825M (± 9.3%) i/s -      9.128M
# shrug via refinement      1.929M (± 6.2%) i/s -      9.651M

# Comparison:
# shrug via refinement:  1928841.5 i/s
#                shrug:  1825069.4 i/s - 1.06x slower


# Does repeated evaluation of the `using` keyword affect performance. Only slightly. 
# This is an unfair test, but I really wanted to force refinements to be slower
# in SOME use case :)
Benchmark.ips do |bm|
  bm.report("inline shrug") { integer_to_shrugs(10) }
  bm.report("inline shrug via refinement") do
    using Shruggable
    10.shrugs
  end
  bm.compare!
end

# Calculating -------------------------------------
#         inline shrug   100.460k i/100ms
# inline shrug via refinement
#                         72.131k i/100ms
# -------------------------------------------------
#         inline shrug      2.507M (± 5.2%) i/s -     12.557M
# inline shrug via refinement
#                           1.498M (± 4.3%) i/s -      7.502M

# Comparison:
#         inline shrug:  2506663.9 i/s
# inline shrug via refinement:  1497747.6 i/s - 1.67x slower

  1. রুবিতে বিটওয়াইজ হ্যাক

  2. রুবিতে ল্যাম্বডাস ব্যবহার করা

  3. রুবিতে সন্নিবেশ বাছাই বোঝা

  4. রুবি 2.6-এ 9টি নতুন বৈশিষ্ট্য