Measure and Benchmark Time for Ruby Methods

后端 未结 6 1018
长情又很酷
长情又很酷 2021-01-30 06:04

How can i measure the time taken by a method and the individual statements in that method in Ruby. If you see the below method i want to measure the total time taken by the meth

6条回答
  •  隐瞒了意图╮
    2021-01-30 06:39

    You could use the Time object. (Time Docs)

    For example,

    start = Time.now
    # code to time
    finish = Time.now
    
    diff = finish - start
    

    diff would be in seconds, as a floating point number.

    EDIT: end is reserved.

提交回复
热议问题