
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Execution Time of Methods in Haskell
In Haskell, we can use getCurrentTime and NominalDiffTime functions to calculate the Execution Time of Methods. In the first example we are going to use (startTime <- getCurrentTime and endTime <- getCurrentTime) function. And in the second example, we are going to use NominalDiffTime function.
Step 1 ? The Data.Time module is imported to work with time.
Step 2 ? The timeFunction function is defined as,
For example 1 ?
timeFunction function = do startTime <- getCurrentTime function endTime <- getCurrentTime.
For example 2 & 3 ?
timeMethod method = do startTime <- getCurrentTime result <- method endTime <- getCurrentTime.
Step 3 ? Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. It calls the timeFunction and someFunction functions.
Step 4 ? The variable named, "diff" is being initialized. It will hold the difference of the start time and end time.
Step 5 ? The resultant execution time is printed to the console, after the function is called.
Example 1
In this example, the timeFunction function takes an IO action as an input, and calculates the time it takes to execute the action. It does this by getting the current time before and after the action is executed, and then calculates the difference. The difference is then printed to the console using the putStrLn function. In this case, someFunction is the method which we are calculating execution time
import Data.Time someFunction :: IO () someFunction = do putStrLn "Function completed" timeFunction :: IO () -> IO () timeFunction function = do startTime <- getCurrentTime function endTime <- getCurrentTime let diff = diffUTCTime endTime startTime putStrLn $ "Execution Time: " ++ (show diff) main :: IO () main = do timeFunction someFunction
Output
Function completed Execution Time: 0.00012295s
Example 2
In this example, we are going to see that how we can calculate the execution time of methods. This can be done by using NominalDiffTime function.
import Data.Time.Clock timeMethod :: IO a -> IO (a, NominalDiffTime) timeMethod method = do startTime <- getCurrentTime result <- method endTime <- getCurrentTime let diffTime = diffUTCTime endTime startTime return (result, diffTime) main :: IO () main = do (result, diffTime) <- timeMethod (putStrLn "Hello, world!") putStrLn ("Execution time: " ++ show diffTime)
Output
Hello, world! Execution time: 0.000132438s
Example 3
In this example, the function uses the Data.Time module to get the current time before and after the given action is executed, and then calculates the difference between the two times to get the execution time. The function returns a tuple of the result of the given action and the execution time.
import Data.Time time :: IO a -> IO (a, NominalDiffTime) time action = do start <- getCurrentTime result <- action end <- getCurrentTime let diff = diffUTCTime end start return (result, diff) main :: IO () main = do (result, timeTaken) <- time (return "Hello World") putStrLn $ "Result: " ++ result putStrLn $ "Time taken: " ++ (show timeTaken)
Output
Result: Hello World Time taken: 0.000000161s
Conclusion
The execution time of a method can be measured in a variety of ways, including using the built-in timer functions in the programming language, by using specialized performance measurement tools or by suing user defined functions. In Haskell, the execution time of the methods can be calculated by using the getCurrentTime and the NominalDiffTime functions.