Main branch.
require 'benchmark/ips'
NUM = 1.12678.freeze
def fast
NUM.round(2).to_s
end
def avg
format('%.2f', NUM)
end
def slow
'%.2f' % NUM
end
Benchmark.ips do |x|
x.report('Float#round') { fast }
x.report('Kernel#format') { avg }
x.report('String#%') { slow }
x.compare!
end
It raises a ActionController::BadRequest (Invalid request parameters: invalid %-encoding) error.
➜ fast-ruby git:(master) ✗ SHARE=1 bundle exec ruby -v code/general/format-vs-round-and-to-s.rb
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin20]
Warming up --------------------------------------
Float#round 154.985k i/100ms
Kernel#format 165.010k i/100ms
String#% 152.993k i/100ms
Calculating -------------------------------------
Float#round 1.547M (± 0.4%) i/s - 7.749M in 5.010822s
Kernel#format 1.659M (± 1.3%) i/s - 8.416M in 5.072483s
String#% 1.540M (± 0.4%) i/s - 7.803M in 5.065333s
Comparison:
Kernel#format: 1659346.4 i/s
Float#round: 1546528.5 i/s - 1.07x (± 0.00) slower
String#%: 1540419.9 i/s - 1.08x (± 0.00) slower
/Users/etagwerker/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/net/http/generic_request.rb:186: warning: net/http: Content-Type did not set; using application/x-www-form-urlencoded
Error sharing report
2021-10-17T16:11:10.437155+00:00 heroku[router]: at=info method=POST path="/reports" host=ips.fastruby.io request_id=bfec470d-de2c-4ab2-a59d-26d41de2cbe9 fwd="71.224.18.32" dyno=web.1 connect=0ms service=4ms status=400 bytes=179 protocol=https
2021-10-17T16:11:10.435586+00:00 app[web.1]: Invalid or incomplete POST params
2021-10-17T16:11:10.435951+00:00 app[web.1]: I, [2021-10-17T16:11:10.435890 #4] INFO -- : [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] Started POST "/reports" for 71.224.18.32 at 2021-10-17 16:11:10 +0000
2021-10-17T16:11:10.437547+00:00 app[web.1]: F, [2021-10-17T16:11:10.437480 #4] FATAL -- : [bfec470d-de2c-4ab2-a59d-26d41de2cbe9]
2021-10-17T16:11:10.437548+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] ActionController::BadRequest (Invalid request parameters: invalid %-encoding ({"entries":[{"name":"Float#round","central_tendency":1546528.508682263,"ips":1546528.508682263,"error":6255,"stddev":6255,"microseconds":5010822.0,"iterations":7749250,"cycles":154985},{"name":"Kernel#format","central_tendency":1659346.446822267,"ips":1659346.446822267,"error":21921,"stddev":21921,"microseconds":5072483.0,"iterations":8415510,"cycles":165010},{"name":"String#%","central_tendency":1540419.907131491,"ips":1540419.907131491,"error":5411,"stddev":5411,"microseconds":5065333.0,"iterations":7802643,"cycles":152993}],"options":{"compare":true}})):
2021-10-17T16:11:10.437548+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9]
2021-10-17T16:11:10.437549+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] vendor/ruby-2.7.2/lib/ruby/2.7.0/uri/common.rb:387:in `decode_www_form_component'
2021-10-17T16:11:10.437549+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/utils.rb:54:in `unescape'
2021-10-17T16:11:10.437550+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/query_parser.rb:155:in `unescape'
2021-10-17T16:11:10.437550+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/query_parser.rb:69:in `block (2 levels) in parse_nested_query'
2021-10-17T16:11:10.437550+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/query_parser.rb:69:in `map!'
2021-10-17T16:11:10.437551+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/query_parser.rb:69:in `block in parse_nested_query'
2021-10-17T16:11:10.437551+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/query_parser.rb:68:in `each'
2021-10-17T16:11:10.437551+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/query_parser.rb:68:in `parse_nested_query'
2021-10-17T16:11:10.437552+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/request.rb:590:in `parse_query'
2021-10-17T16:11:10.437552+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/request.rb:454:in `POST'
2021-10-17T16:11:10.437552+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/request.rb:400:in `block (2 levels) in POST'
2021-10-17T16:11:10.437553+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/parameters.rb:90:in `block in parse_formatted_parameters'
2021-10-17T16:11:10.437553+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/parameters.rb:90:in `fetch'
2021-10-17T16:11:10.437553+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/parameters.rb:90:in `parse_formatted_parameters'
2021-10-17T16:11:10.437553+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/request.rb:399:in `block in POST'
2021-10-17T16:11:10.437554+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/request.rb:69:in `fetch'
2021-10-17T16:11:10.437554+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/request.rb:69:in `fetch_header'
2021-10-17T16:11:10.437554+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/request.rb:398:in `POST'
2021-10-17T16:11:10.437554+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/parameters.rb:55:in `parameters'
2021-10-17T16:11:10.437555+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/filter_parameters.rb:43:in `filtered_parameters'
2021-10-17T16:11:10.437555+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_controller/metal/instrumentation.rb:24:in `process_action'
2021-10-17T16:11:10.437555+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_controller/metal/params_wrapper.rb:249:in `process_action'
2021-10-17T16:11:10.437555+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] activerecord (6.1.4) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
2021-10-17T16:11:10.437555+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/abstract_controller/base.rb:165:in `process'
2021-10-17T16:11:10.437556+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionview (6.1.4) lib/action_view/rendering.rb:39:in `process'
2021-10-17T16:11:10.437556+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_controller/metal.rb:190:in `dispatch'
2021-10-17T16:11:10.437556+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_controller/metal.rb:254:in `dispatch'
2021-10-17T16:11:10.437556+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
2021-10-17T16:11:10.437556+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:33:in `serve'
2021-10-17T16:11:10.437558+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/journey/router.rb:50:in `block in serve'
2021-10-17T16:11:10.437559+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/journey/router.rb:32:in `each'
2021-10-17T16:11:10.437559+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/journey/router.rb:32:in `serve'
2021-10-17T16:11:10.437559+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/routing/route_set.rb:842:in `call'
2021-10-17T16:11:10.437559+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
2021-10-17T16:11:10.437559+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/etag.rb:27:in `call'
2021-10-17T16:11:10.437559+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/conditional_get.rb:40:in `call'
2021-10-17T16:11:10.437559+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/head.rb:12:in `call'
2021-10-17T16:11:10.437560+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/permissions_policy.rb:22:in `call'
2021-10-17T16:11:10.437560+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
2021-10-17T16:11:10.437560+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
2021-10-17T16:11:10.437560+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
2021-10-17T16:11:10.437560+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/cookies.rb:689:in `call'
2021-10-17T16:11:10.437560+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
2021-10-17T16:11:10.437561+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] activesupport (6.1.4) lib/active_support/callbacks.rb:98:in `run_callbacks'
2021-10-17T16:11:10.437561+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
2021-10-17T16:11:10.437561+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
2021-10-17T16:11:10.437561+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
2021-10-17T16:11:10.437561+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
2021-10-17T16:11:10.437561+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] railties (6.1.4) lib/rails/rack/logger.rb:37:in `call_app'
2021-10-17T16:11:10.437561+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] railties (6.1.4) lib/rails/rack/logger.rb:26:in `block in call'
2021-10-17T16:11:10.437562+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] activesupport (6.1.4) lib/active_support/tagged_logging.rb:99:in `block in tagged'
2021-10-17T16:11:10.437562+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] activesupport (6.1.4) lib/active_support/tagged_logging.rb:37:in `tagged'
2021-10-17T16:11:10.437562+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] activesupport (6.1.4) lib/active_support/tagged_logging.rb:99:in `tagged'
2021-10-17T16:11:10.437562+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] railties (6.1.4) lib/rails/rack/logger.rb:26:in `call'
2021-10-17T16:11:10.437562+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
2021-10-17T16:11:10.437562+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/request_id.rb:26:in `call'
2021-10-17T16:11:10.437563+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/method_override.rb:24:in `call'
2021-10-17T16:11:10.437563+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/runtime.rb:22:in `call'
2021-10-17T16:11:10.437563+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] activesupport (6.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
2021-10-17T16:11:10.437563+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
2021-10-17T16:11:10.437563+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/static.rb:24:in `call'
2021-10-17T16:11:10.437563+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
2021-10-17T16:11:10.437564+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] actionpack (6.1.4) lib/action_dispatch/middleware/host_authorization.rb:92:in `call'
2021-10-17T16:11:10.437564+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] railties (6.1.4) lib/rails/engine.rb:539:in `call'
2021-10-17T16:11:10.437564+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] puma (4.3.8) lib/puma/configuration.rb:228:in `call'
2021-10-17T16:11:10.437564+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] puma (4.3.8) lib/puma/server.rb:718:in `handle_request'
2021-10-17T16:11:10.437564+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] puma (4.3.8) lib/puma/server.rb:472:in `process_client'
2021-10-17T16:11:10.437567+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] puma (4.3.8) lib/puma/server.rb:328:in `block in run'
2021-10-17T16:11:10.437567+00:00 app[web.1]: [bfec470d-de2c-4ab2-a59d-26d41de2cbe9] puma (4.3.8) lib/puma/thread_pool.rb:134:in `block in spawn_thread'