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

Use tr instead of gsub #149

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chaymaeBZ
Copy link

tr is cheaper to run than gsub

Here is a little benchmark example :

require 'benchmark'

test_string = "namespace/example_services"

Benchmark.bm(10) do |x|
  x.report("gsub:") do
    1_000_000.times do
      test_string.gsub(/\//, "--").gsub("_", "-")
    end
  end

  x.report("tr:") do
    1_000_000.times do
      test_string.gsub(/\//, "--").tr("_", "-")
    end
  end
end
                 user     system      total        real
gsub:        1.128953   0.006334   1.135287 (  1.135361)
tr:          0.867549   0.004827   0.872376 (  0.872377)

`tr` is cheaper to run than `gsub`
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.

1 participant