Skip to content
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

scalar_scanner.rb - delete is typically 4 to 5 times faster than gsub #530

Merged
merged 1 commit into from
May 9, 2022

Conversation

MSP-Greg
Copy link
Contributor

@MSP-Greg MSP-Greg commented Dec 25, 2021

Ok, locally on Windows, 4 to 5 times faster, with WSL2/Ubuntu, 3 times.

@MSP-Greg
Copy link
Contributor Author

Re benchmarks, I used:

require 'benchmark'

n = 500_000

INT = %w[123,456 123_456 123,_456_ 123,_456, 123,_456_, 123_,456_, ]

FLOAT = %w[123,456 123,456.001_02 123_456 123_456.001_02]

puts "#{'-' * 50} Integer Test"

Benchmark.bmbm do |x|
  x.report("gsub") {
    n.times { INT.each { |s| s.gsub(/,_/, '') } }
  }
  x.report("tr") {
    n.times { INT.each { |s| s.tr ',_', '' } }
  }
end

puts "\n#{'-' * 50} Float Test"

Benchmark.bmbm do |x|
  x.report("gsub") {
    n.times { FLOAT.each { |s| s.gsub(/[,_]|\.([Ee]|$)/, '\1') } }
  }
  x.report("tr") {
    n.times { FLOAT.each { |s| s.tr(',_', '').gsub(/\.([Ee]|$)/, '\1') } }
  }
end

lib/psych/scalar_scanner.rb Outdated Show resolved Hide resolved
lib/psych/scalar_scanner.rb Outdated Show resolved Hide resolved
@MSP-Greg MSP-Greg changed the title scalar_scanner.rb - tr is typically 4 to 5 times faster than gsub scalar_scanner.rb - delete is typically 4 to 5 times faster than gsub Mar 29, 2022
@tenderlove tenderlove merged commit 4400c0c into ruby:master May 9, 2022
@MSP-Greg
Copy link
Contributor Author

MSP-Greg commented May 9, 2022

@tenderlove

Thanks. JFYI, in the Actions Windows workflow (windows.yml), MSP-Greg/setup-ruby-pkgs is used. It is now ruby/setup-ruby-pkgs.

But, for Windows jobs using windows-latest (windows-2022) image, all the dependencies for mingw/ucrt builds are installed by ruby/setup-ruby. Hopefully, the same will be done soon for mswin builds, so it can then be changed to ruby/setup-ruby...

@MSP-Greg MSP-Greg deleted the gsub2tr branch May 9, 2022 18:29
@tenderlove
Copy link
Member

@MSP-Greg cool, thanks for the information! Also thanks for the PR 🙇🏻‍♀️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants