8000 Use String.build to format_data in profile by veelenga · Pull Request #10 · tbrand/router.cr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use String.build to format_data in profile #10

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

Merged
merged 1 commit into from
Apr 13, 2017

Conversation

veelenga
Copy link
Contributor
@veelenga veelenga commented Apr 13, 2017

String.build is a de-facto string builder in Crystal. Much more faster than the default string concatenation:

require "benchmark"

def concatenate
  res = ""
  1000.times do |i|
    res += "#{i}"
  end
end

def build
  String.build do |res|
    1000.times do |i|
      res << "#{i}"
    end
  end
end

Benchmark.ips do |x|
  x.report("concatenate") { concatenate }
  x.report("build") { build }
end
$ crystal string_concatenation.cr --release
concatenate 631.46  (  1.58ms) (± 2.02%)  8.28× slower
      build   5.23k (191.17µs) (± 1.24%)       fastest

String.build is a defacto string builder in Crystal.
Much more faster than default string concatenation.
@tbrand
Copy link
Owner
tbrand commented Apr 13, 2017

Awesome:tada:

@tbrand tbrand merged commit c123f05 into tbrand:master Apr 13, 2017
@veelenga veelenga deleted the feature/profiler_improvement branch April 13, 2017 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0