Ruby Benchmark库

踉踉跄跄
• 阅读 3399

Benchmark模块提供了一些方法来测量和报告Ruby代码的执行时间。

下面我们来测量"a"*1_000_000的执行时间:

require 'benchmark'

puts Benchmark.measure { "a"*1_000_000 }

结果如下(我的电脑是i3,linux mint操作系统):

xiongxin@rubyonrails ~/ruby/6-7 $ ruby benchmark-t.rb 
  0.000000   0.000000   0.000000 (  0.000599)

下面Benchmark模块提供measure方法的代码

  def measure(label = "") # :yield:    
    t0, r0 = Process.times, Time.now
    yield
    t1, r1 = Process.times, Time.now
    Benchmark::Tms.new(t1.utime  - t0.utime,
                       t1.stime  - t0.stime,
                       t1.cutime - t0.cutime,
                       t1.cstime - t0.cstime,
                       r1 - r0,
                       label)
  end

注释:
Process.times 返回 Tms structure,包括用户和系统CPU时间

  t = Process.times
  #其实现在 t = [ t.utime, t.stime, t.cutime, t.cstime ]

我们来一个一个分析他的返回值:
Struct::Tms#cstime() #=> Float
Returns the number of seconds of system CPU time consumed by waited-for, terminated child processes, i.e. the sum of their Struct::Tms#stime and Struct::Tms#cstime values. Returns 0.0 on Windows.

Struct::Tms#cutime() #=> Float
Returns the number of seconds of user CPU time consumed by waited-for, terminated child processes, i.e. the sum of their Struct::Tms#utime and Struct::Tms#cutime values. Returns 0.0 on Windows.

Struct::Tms#stime() #=> Float
Returns the number of seconds of system CPU time consumed by the calling process.

Struct::Tms#utime() #=> Float
Returns the number of seconds of user CPU time consumed by the calling process.

点赞
收藏
评论区
推荐文章
雷厉风行 雷厉风行
2年前
Mac用户必备的Ruby开发工具,RubyMine 2023新版 永久激活码
RubyMine2023forMac是JetBrains公司开发的一款Ruby编程语言开发工具,支持多种Ruby开发环境,包括Rails、Sinatra等。该软件为Ruby开发人员提供了一站式的集成开发环境(IDE),可帮助他们更快地、更高效地开发代码。
雷厉风行 雷厉风行
2年前
RubyMine 2023 for Mac:专业、高效的 Ruby 开发工具,支持多种 Ruby 版本和框架
RubyMine2023forMac是一款由JetBrains公司开发的专业Ruby开发环境。它提供了许多强大的工具和功能,可以帮助Ruby开发者更高效地编写、调试和部署Ruby代码。mac软件下载:RubyMine2023forMac的主要特点包括:1.
雷厉风行 雷厉风行
2年前
Mac程序员软件-RubyMine 2022 for Mac(强大的Rails/Ruby开发工具)完美激活版
RubyMine2022forMac是一款专注于Ruby开发的综合开发环境。它提供了一系列功能和工具,可用于各种Ruby开发任务,如代码编辑、调试、测试、版本控制和代码浏览等。
Stella981 Stella981
3年前
Sass预编译
sass基于Ruby       ruby的模块管理器叫gem 正规的安装方式是geminstallsass        调用命令  sass  sass文件  css文件webpack编译sass    需要的模块styleloader cssloader sas
Stella981 Stella981
3年前
Ruby on Rails 学习笔记(三)
从模型开始,先建立模型而不是建立支架 ruby script/generate model ad name:string description:text price:decimal seller_id:integer email:string img_rul:string从新迁移一下数据库rake db:migrate空模型
Stella981 Stella981
3年前
Python技巧之测量执行时间
\使用"timeit"模块可以测量短小代码的执行时间下面代码对比了三种方法拼接100以内的数字的执行时间每段主代码均执行了10000次importtimeittimeit.timeit('"".join(str(n)forninrange(100))',n
Stella981 Stella981
3年前
Ruby中的Profiling工具
看看如何调试Ruby的性能问题Ruby内置的profiler内置的profiler实现的很简单,在ruby2.2中只有150行代码,大家可以看看它的实现profile.rb(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fruby%2Fruby
子桓 子桓
2年前
Ruby代码编辑器 RubyMine 2023 mac
RubyMine2023mac一款适用于各种Ruby项目的智能IDE,具有智能代码编辑器,专为高效开发而设计。1.智能和简单的编码代码完成为Ruby和RAIls,JavaScript和CoffeeScript,ERB和HAML,CSS,Sass和Less等
燕青 燕青
1年前
RubyMine 2023 Mac中文版 附激活密钥
是JetBrains开发的一款为Ruby开发者量身定制的集成开发环境(IDE)。它为Ruby语言提供了全面的支持,包括代码编辑、调试、测试和集成版本控制系统等功能,帮助开发者更加高效地进行Ruby编程。在RubyMine2023中,有一些核心的功能和特性,
绣鸾 绣鸾
1年前
RubyMine 2023 for Mac(Ruby代码编辑器)
是Ruby编程语言的集成开发环境(IDE)。它提供了一系列功能来帮助开发人员编写、分析和调试Ruby代码。RubyMine的一些主要功能包括:具有语法突出显示、自动完成和错误突出显示的智能代码编辑器支持Git、SVN和Mercurial等版本控制系统自动化
公孙晃 公孙晃
1年前
RubyMine 2023.2.4中文激活版 附激活码 无需账号登录
是Ruby编程语言的集成开发环境(IDE)。它提供了一系列功能来帮助开发人员编写、分析和调试Ruby代码。RubyMine的一些主要功能包括:具有语法突出显示、自动完成和错误突出显示的智能代码编辑器支持Git、SVN和Mercurial等版本控制系统自动化
踉踉跄跄
踉踉跄跄
Lv1
何处秋风至?萧萧送雁群。
文章
3
粉丝
0
获赞
0