Skip to content

Cookbook Date and Time : Measure Elapsed Time#3447

Open
Adamme-le-codeur-fou wants to merge 1 commit intoocaml:mainfrom
Adamme-le-codeur-fou:main
Open

Cookbook Date and Time : Measure Elapsed Time#3447
Adamme-le-codeur-fou wants to merge 1 commit intoocaml:mainfrom
Adamme-le-codeur-fou:main

Conversation

@Adamme-le-codeur-fou
Copy link

added the recipe 'Measure Elapsed Time' (uses Unix.gettimeofday)

Copilot AI review requested due to automatic review settings January 9, 2026 12:43
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. *)
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Suggested change
(* 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. *)

Copilot uses AI. Check for mistakes.
Comment on lines +18 to +24
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);
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
Comment on lines +18 to +24
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);
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants