-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the Style/SelectByRegexp
cop
#12459
Comments
I have the Dockerfile: FROM ruby:3.3.0-preview3
COPY . /var/www/ruby
WORKDIR /var/www/ruby
RUN gem install benchmark-ips
RUN ruby -v
CMD ["ruby", "file.rb"] |
1 caserequire "benchmark/ips"
ARRAY = ("a".."zzz").to_a.shuffle.freeze
def initial
ARRAY.select { |x| x.match?(/regexp/) }
end
def slow
ARRAY.select { |x| x.include?('regexp') }
end
def fast
ARRAY.grep(/regexp/)
end
Benchmark.ips do |x|
x.report('initial') { initial }
x.report('Array#select#include') { slow }
x.report('Array#grep') { fast }
x.compare!
end Output: ydakuka@yauhenid:~/ruby-docker-app$ docker run ruby-app
Warming up --------------------------------------
initial 60.000 i/100ms
Array#select#include 72.000 i/100ms
Array#grep 95.000 i/100ms
Calculating -------------------------------------
initial 582.673 (± 4.8%) i/s - 2.940k in 5.058520s
Array#select#include 777.983 (± 1.4%) i/s - 3.960k in 5.091249s
Array#grep 966.763 (± 2.5%) i/s - 4.845k in 5.015105s
Comparison:
Array#grep: 966.8 i/s
Array#select#include: 778.0 i/s - 1.24x slower
initial: 582.7 i/s - 1.66x slower |
2 caserequire "benchmark/ips"
ARRAY = ("a".."zzz").to_a.shuffle.freeze
def initial
ARRAY.reject { |x| x.match?(/regexp/) }
end
def slow
ARRAY.reject { |x| x.include?('regexp') }
end
def fast
ARRAY.grep_v(/regexp/)
end
Benchmark.ips do |x|
x.report('initial') { initial }
x.report('Array#reject#include') { slow }
x.report('Array#grep_v') { fast }
x.compare!
end Output: ydakuka@yauhenid:~/ruby-docker-app$ docker run ruby-app
Warming up --------------------------------------
initial 42.000 i/100ms
Array#reject#include 44.000 i/100ms
Array#grep_v 76.000 i/100ms
Calculating -------------------------------------
initial 504.497 (± 6.5%) i/s - 2.520k in 5.020388s
Array#reject#include 637.544 (± 8.0%) i/s - 3.168k in 5.025849s
Array#grep_v 792.533 (± 2.4%) i/s - 4.028k in 5.085471s
Comparison:
Array#grep_v: 792.5 i/s
Array#reject#include: 637.5 i/s - 1.24x slower
initial: 504.5 i/s - 1.57x slower |
ydakuka
changed the title
[WIP] Improve the
Improve the Dec 3, 2023
Style/SelectByRegexp
copStyle/SelectByRegexp
cop
I think this should be implemented as a separate cop. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the solution you'd like
I have the following code:
If I run the command
rubocop -A --only Style/SelectByRegexp
I will get:However, if I run the command
rubocop -A --only Performance/StringInclude
I will get:So I conclude that these onelines do the same thing.
I propose (see benchmarks for more information):
The text was updated successfully, but these errors were encountered: