Refactoring RedAmber - some benchmarks #139
Replies: 9 comments
-
Check if duplicated key is exist (重複しているキーがあるかどうか調べる)Benchmark.driver do |x|
x.prelude <<~RUBY
@keys = %w[year month day dep_time sched_dep_time dep_delay \
arr_time sched_arr_time arr_delay carrier flight tailnum \
origin dest air_time distance hour minute time_hour carrer tailnum origin dest]
def count
duplicated_keys = @keys.select { |k| @keys.count(k) > 1 }.uniq
duplicated_keys.empty?
end
def uniq
duplicated_keys = @keys.tally.select { |_k, v| v > 1 }.keys
duplicated_keys.empty?
end
def equal
@keys == @keys.uniq
end
def uniq!
@keys.uniq!
end
RUBY
x.report 'count'
x.report 'uniq'
x.report 'equal'
x.report 'uniq!'
end; nil
エラーメッセージの表示に使うために重複しているキーを残しておきたいので、 |
Beta Was this translation helpful? Give feedback.
-
lambda in pattern matchingBenchmark.driver do |x|
x.prelude <<~RUBY
require 'arrow-numo-narray'
@a = Numo::DFloat.new(3).rand_norm
def lambda
case @a
in ->(x) { x.respond_to?(:to_arrow_array) } => arrowable
arrowable.to_arrow_array
else
end
end
def if_clause
case @a
in arrowable if arrowable.respond_to?(:to_arrow_array)
arrowable.to_arrow_array
else
end
end
RUBY
x.report 'lambda'
x.report 'if_clause'
end; nil
パターンマッチで後置のifを置くのと Putting a post if in pattern matching and using |
Beta Was this translation helpful? Give feedback.
-
Get the index where true is located (trueがある位置のインデックスを得る)require 'benchmark_driver'
Benchmark.driver do |x|
x.prelude <<~RUBY
@booleans = [true, false, true, false, nil] * 100
def select
([email protected]).select.with_index { |_, i| @booleans[i] }
end
def each_with_object1
@booleans.each_with_object([]).with_index { |(x, a), i| a << i if x }
end
RUBY
x.report %{ select }
x.report %{ each_with_object }
end;nil
|
Beta Was this translation helpful? Give feedback.
-
Drop elements from an Array (Arrayの要素を取り除く)Benchmark.driver do |x|
x.prelude <<~RUBY
@keys = (:a..).take(20)
@dropper = [:a, :c, :g, :p, :q]
def reject
@keys.reject { |k| @dropper.include?(k) }
end
def subtruct
@keys - @dropper
end
RUBY
x.report 'reject'
x.report 'subtruct'
end; nil
|
Beta Was this translation helpful? Give feedback.
-
reject_columns (列を取り除く)require 'benchmark_driver'
Benchmark.driver do |x|
x.prelude <<~RUBY
require 'arrow'
@table = Arrow::Table.new [
["id", [1, 2, 3, nil]],
["region_id", [7, 8, 9, nil]],
["description", ["A", "B", "C", nil]],
["id", [1, 2, nil, 4]],
["description", ["D", "E", nil, "F"]]
]
@rejector = [3, 4]
def reject_index
@table.columns.reject.with_index { |_, i| @rejector.include? i }
end
def delete_at
c = @table.columns
@rejector.each_with_index { |pos, i| c.delete_at(pos - i) }
c
end
RUBY
x.report 'reject_index'
x.report 'delete_at'
end; nil
この計測はcolumnの配列を生成するところまでで比較していて、この後テーブルを生成するとそちらの方が時間がかかるので4%程の差にとどまります。 This measurement compares only the generation of the column array, generating table takes more time. The difference is only about 4% when generating Table by them. |
Beta Was this translation helpful? Give feedback.
-
Table#column_namesテーブルの要素の取り出し. How to create a list of column names. Benchmark.driver do |x|
x.prelude <<~RUBY
require 'datasets-arrow'
@table = Datasets::Penguins.new.to_arrow
def schema
@table.schema.fields.map(&:name)
end
def columns
@table.columns.map(&:name)
end
RUBY
x.report 'schema'
x.report 'columns'
end; nil
|
Beta Was this translation helpful? Give feedback.
-
How to check if
|
Beta Was this translation helpful? Give feedback.
-
Create a DataFramerequire 'benchmark_driver'
Benchmark.driver do |x|
x.prelude <<~RUBY
require './red_amber'
include RedAmber
require 'datasets-arrow'
@table = Datasets::Penguins.new.to_arrow
@keys = [:species, :island]
def by_new
DataFrame.new(@table)
end
def by_create
DataFrame.create(@table)
end
RUBY
x.report 'by_new'
x.report 'by_create'
end; nil
|
Beta Was this translation helpful? Give feedback.
-
Create VectorBenchmark.driver do |x|
x.prelude <<~PRE
require 'red_amber'
include RedAmber
require 'datasets-arrow'
penguins = DataFrame.new(Datasets::Penguins.new)
table = penguins.table
func = Arrow::Function.find(:is_null)
PRE
x.report %{ penguins.body_mass_g }
x.report %{ penguins[:body_mass_g] }
x.report %{ penguins.v(:body_mass_g) }
x.report %{ penguins.variables[:body_mass_g] }
x.report %{ penguins.table[:body_mass_g] }
end;nil
|
Beta Was this translation helpful? Give feedback.
-
私は今RedAmberをもう少し高速化するためにリファクタリングに取り組んでいます。
ここではその過程で比較したベンチマークの断片を紹介していきます。
I am currently working on refactoring RedAmber to make it a bit faster. Here are some of the benchmark fragments I compared in the process.
Beta Was this translation helpful? Give feedback.
All reactions