{"id":13877886,"url":"https://github.com/ruby/benchmark","last_synced_at":"2025-05-14T10:08:21.104Z","repository":{"id":38457856,"uuid":"200768946","full_name":"ruby/benchmark","owner":"ruby","description":"The Benchmark module provides methods for benchmarking Ruby code, giving detailed reports on the time taken for each task.","archived":false,"fork":false,"pushed_at":"2025-02-25T04:09:33.000Z","size":143,"stargazers_count":172,"open_issues_count":5,"forks_count":22,"subscribers_count":34,"default_branch":"master","last_synced_at":"2025-04-03T20:48:41.220Z","etag":null,"topics":["ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ruby.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-06T03:24:01.000Z","updated_at":"2025-03-26T14:36:20.000Z","dependencies_parsed_at":"2024-05-05T18:25:42.072Z","dependency_job_id":"bf0cbc0f-8f41-4a63-aa92-10e73cf1e758","html_url":"https://github.com/ruby/benchmark","commit_stats":{"total_commits":95,"total_committers":24,"mean_commits":"3.9583333333333335","dds":0.7157894736842105,"last_synced_commit":"1cb57866a35e22e215eb12fce6f02bfc1a1bb0d6"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fbenchmark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fbenchmark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fbenchmark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Fbenchmark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruby","download_url":"https://codeload.github.com/ruby/benchmark/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248338772,"owners_count":21087210,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ruby"],"created_at":"2024-08-06T08:01:34.026Z","updated_at":"2025-04-11T03:39:15.887Z","avatar_url":"https://github.com/ruby.png","language":"Ruby","readme":"# Benchmark\n\nThe Benchmark module provides methods for benchmarking Ruby code, giving detailed reports on the time taken for each task.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'benchmark'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install benchmark\n\n## Usage\n\nThe Benchmark module provides methods to measure and report the time used to execute Ruby code.\n\nMeasure the time to construct the string given by the expression \u003ccode\u003e\"a\"*1_000_000_000\u003c/code\u003e:\n\n```ruby\nrequire 'benchmark'\nputs Benchmark.measure { \"a\"*1_000_000_000 }\n```\n\nOn my machine (OSX 10.8.3 on i5 1.7 GHz) this generates:\n\n```\n0.350000   0.400000   0.750000 (  0.835234)\n```\n\nThis report shows the user CPU time, system CPU time, the sum of the user and system CPU times, and the elapsed real time. The unit of time is seconds.\n\nDo some experiments sequentially using the #bm method:\n\n```ruby\nrequire 'benchmark'\nn = 5000000\nBenchmark.bm do |x|\n  x.report { for i in 1..n; a = \"1\"; end }\n  x.report { n.times do   ; a = \"1\"; end }\n  x.report { 1.upto(n) do ; a = \"1\"; end }\nend\n```\n\nThe result:\n\n```\n    user     system      total        real\n1.010000   0.000000   1.010000 (  1.014479)\n1.000000   0.000000   1.000000 (  0.998261)\n0.980000   0.000000   0.980000 (  0.981335)\n```\n\nContinuing the previous example, put a label in each report:\n\n```ruby\nrequire 'benchmark'\nn = 5000000\nBenchmark.bm(7) do |x|\n  x.report(\"for:\")   { for i in 1..n; a = \"1\"; end }\n  x.report(\"times:\") { n.times do   ; a = \"1\"; end }\n  x.report(\"upto:\")  { 1.upto(n) do ; a = \"1\"; end }\nend\n```\n\nThe result:\n\n```\n              user     system      total        real\nfor:      1.010000   0.000000   1.010000 (  1.015688)\ntimes:    1.000000   0.000000   1.000000 (  1.003611)\nupto:     1.030000   0.000000   1.030000 (  1.028098)\n```\n\nThe times for some benchmarks depend on the order in which items are run.  These differences are due to the cost of memory allocation and garbage collection. To avoid these discrepancies, the #bmbm method is provided.  For example, to compare ways to sort an array of floats:\n\n```ruby\nrequire 'benchmark'\narray = (1..1000000).map { rand }\nBenchmark.bmbm do |x|\n  x.report(\"sort!\") { array.dup.sort! }\n  x.report(\"sort\")  { array.dup.sort  }\nend\n```\n\nThe result:\n\n```\nRehearsal -----------------------------------------\nsort!   1.490000   0.010000   1.500000 (  1.490520)\nsort    1.460000   0.000000   1.460000 (  1.463025)\n-------------------------------- total: 2.960000sec\n            user     system      total        real\nsort!   1.460000   0.000000   1.460000 (  1.460465)\nsort    1.450000   0.010000   1.460000 (  1.448327)\n```\n\nReport statistics of sequential experiments with unique labels, using the #benchmark method:\n\n```ruby\nrequire 'benchmark'\ninclude Benchmark         # we need the CAPTION and FORMAT constants\nn = 5000000\nBenchmark.benchmark(CAPTION, 7, FORMAT, \"\u003etotal:\", \"\u003eavg:\") do |x|\n  tf = x.report(\"for:\")   { for i in 1..n; a = \"1\"; end }\n  tt = x.report(\"times:\") { n.times do   ; a = \"1\"; end }\n  tu = x.report(\"upto:\")  { 1.upto(n) do ; a = \"1\"; end }\n  [tf+tt+tu, (tf+tt+tu)/3]\nend\n```\n\nThe result:\n\n```\n             user     system      total        real\nfor:      0.950000   0.000000   0.950000 (  0.952039)\ntimes:    0.980000   0.000000   0.980000 (  0.984938)\nupto:     0.950000   0.000000   0.950000 (  0.946787)\n\u003etotal:   2.880000   0.000000   2.880000 (  2.883764)\n\u003eavg:     0.960000   0.000000   0.960000 (  0.961255)\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/ruby/benchmark.\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby%2Fbenchmark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruby%2Fbenchmark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby%2Fbenchmark/lists"}