Actions
Bug #15929
closedArray#minmax is much slower than calling both #min and #max
Bug #15929:
Array#minmax is much slower than calling both #min and #max
Description
this is similar to issue 15807 about Ranges and maybe also to 13917
current situation:
- calling
Array#minmaxincurs a performance penalty of almost 50% compared to calling both#minand#max
require 'benchmark/ips'
arr = (1..1000).map { rand }
Benchmark.ips do |x|
x.report('min, max') { [arr.min, arr.max] }
x.report('minmax') { arr.minmax }
end
min, max 53.832k (± 1.8%) i/s - 270.861k in 5.033263s
minmax 30.093k (± 1.2%) i/s - 151.980k in 5.051078s
background:
-
#minmaxis included viaEnumerable -
Enumerable#minmaxdoes not call array's optimized#minand#maximplementations
possible solutions:
- a) change
Enumerable#minmaxand let itrb_funcallminandmaxas suggested here (will also fix 15807) - b) implement minmax in array.c to call
rb_ary_minandrb_ary_max
Files
Actions