Cookbook Date and Time : Measure Elapsed Time#3447
Cookbook Date and Time : Measure Elapsed Time#3447Adamme-le-codeur-fou wants to merge 1 commit intoocaml:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new cookbook recipe demonstrating how to measure elapsed time in OCaml using the Unix.gettimeofday function from the standard library.
- Introduces a basic example of timing code execution
- Provides a reusable helper function for timing arbitrary operations
- Demonstrates tail-recursive functions as test cases for timing
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Printf.printf "Counting to %d...\n" n; | ||
| aux 0 | ||
|
|
||
| (* We use the `Unix` library (which is part of OCaml's standard library), which provides the function `Unix.gettimeofday` to get the current time in seconds since the Unix epoch. *) |
There was a problem hiding this comment.
The comment has awkward repetition of "which". Consider rephrasing to: "We use the Unix library (part of OCaml's standard library) to access Unix.gettimeofday, which gets the current time in seconds since the Unix epoch."
| (* We use the `Unix` library (which is part of OCaml's standard library), which provides the function `Unix.gettimeofday` to get the current time in seconds since the Unix epoch. *) | |
| (* We use the `Unix` library (part of OCaml's standard library) to access `Unix.gettimeofday`, which gets the current time in seconds since the Unix epoch. *) |
| Printf.printf "Elapsed time: %f seconds\n" (Unix.gettimeofday () -. start_time) | ||
|
|
||
| (* We can also create a function that executes another function and prints the elapsed time *) | ||
| let exec_and_print_elapsed_time f name = | ||
| let start_time = Unix.gettimeofday () in | ||
| let result = f () in | ||
| Printf.printf "Elapsed time for %s: %f seconds\n" name (Unix.gettimeofday () -. start_time); |
There was a problem hiding this comment.
Calling Unix.gettimeofday () twice may introduce a small timing error. It's better to capture the end time in a variable first, then compute the elapsed time. This ensures the measurement is more accurate.
| Printf.printf "Elapsed time: %f seconds\n" (Unix.gettimeofday () -. start_time) | |
| (* We can also create a function that executes another function and prints the elapsed time *) | |
| let exec_and_print_elapsed_time f name = | |
| let start_time = Unix.gettimeofday () in | |
| let result = f () in | |
| Printf.printf "Elapsed time for %s: %f seconds\n" name (Unix.gettimeofday () -. start_time); | |
| let end_time = Unix.gettimeofday () in | |
| Printf.printf "Elapsed time: %f seconds\n" (end_time -. start_time) | |
| (* We can also create a function that executes another function and prints the elapsed time *) | |
| let exec_and_print_elapsed_time f name = | |
| let start_time = Unix.gettimeofday () in | |
| let result = f () in | |
| let end_time = Unix.gettimeofday () in | |
| Printf.printf "Elapsed time for %s: %f seconds\n" name (end_time -. start_time); |
| Printf.printf "Elapsed time: %f seconds\n" (Unix.gettimeofday () -. start_time) | ||
|
|
||
| (* We can also create a function that executes another function and prints the elapsed time *) | ||
| let exec_and_print_elapsed_time f name = | ||
| let start_time = Unix.gettimeofday () in | ||
| let result = f () in | ||
| Printf.printf "Elapsed time for %s: %f seconds\n" name (Unix.gettimeofday () -. start_time); |
There was a problem hiding this comment.
Calling Unix.gettimeofday () twice may introduce a small timing error. It's better to capture the end time in a variable first, then compute the elapsed time. This ensures the measurement is more accurate.
| Printf.printf "Elapsed time: %f seconds\n" (Unix.gettimeofday () -. start_time) | |
| (* We can also create a function that executes another function and prints the elapsed time *) | |
| let exec_and_print_elapsed_time f name = | |
| let start_time = Unix.gettimeofday () in | |
| let result = f () in | |
| Printf.printf "Elapsed time for %s: %f seconds\n" name (Unix.gettimeofday () -. start_time); | |
| let end_time = Unix.gettimeofday () in | |
| Printf.printf "Elapsed time: %f seconds\n" (end_time -. start_time) | |
| (* We can also create a function that executes another function and prints the elapsed time *) | |
| let exec_and_print_elapsed_time f name = | |
| let start_time = Unix.gettimeofday () in | |
| let result = f () in | |
| let end_time = Unix.gettimeofday () in | |
| Printf.printf "Elapsed time for %s: %f seconds\n" name (end_time -. start_time); |
added the recipe 'Measure Elapsed Time' (uses Unix.gettimeofday)