![]() |
In this article, we will learn about how to time an operation in milliseconds in Ruby. We can perform this using different approaches. Table of Content Using Benchmark.realtimeBenchmark.realtime method to measure the time taken for the operation inside the block. Inside the block, we perform the operation we want to do. This method returns the time taken in seconds. We multiply it by 1000 to convert it into milliseconds. Syntax:
Below is the Ruby program to convert into milliseconds using Benchmark.realtime:
Output Time taken: 500.54 milliseconds Using Process.clock_gettimeProcess.clock_gettime calculates the difference between start time and end time. It records the current time before performing the operation and after performing the operation using Process.clock_gettime(Process::CLOCK_MONOTIC) . This method returns the time taken in seconds. We multiply it by 1000 to convert into milliseconds. Syntax:
Below is the Ruby program to convert into milliseconds using Process.clock_gettime:
Output Time taken: 500.56117499980246 milliseconds Using Time classThis approach is similar to the previous aprroach. In Time class method we have Time.now method using which we can calculate the current time before performing the operation and after performing the operation. Then we calculate the difference between them. It returns the result in seconds. We multiply it by 1000 to convert into milliseconds. Syntax :
Below is the Ruby program to convert into milliseconds Using Time class:
Output Time taken: 500.386731 milliseconds |
Reffered: https://www.geeksforgeeks.org
Ruby |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |