Skip to main content
ubuntuask.com

Back to all posts

How to Get A Microsecond From Now() In Erlang?

Published on
5 min read
How to Get A Microsecond From Now() In Erlang? image

Best Erlang Programming Tools to Buy in October 2025

1 Erlang Programming: A Concurrent Approach to Software Development

Erlang Programming: A Concurrent Approach to Software Development

BUY & SAVE
$39.99
Erlang Programming: A Concurrent Approach to Software Development
2 Programming Erlang: Software for a Concurrent World

Programming Erlang: Software for a Concurrent World

BUY & SAVE
$35.99
Programming Erlang: Software for a Concurrent World
3 Introducing Erlang: Getting Started in Functional Programming

Introducing Erlang: Getting Started in Functional Programming

BUY & SAVE
$33.99
Introducing Erlang: Getting Started in Functional Programming
4 Rust: Rust Programming, In 8 Hours, For Beginners, Learn Coding Fast: Rust Language, Crash Course Textbook & Exercises (Cookbooks in 8 Hours 15)

Rust: Rust Programming, In 8 Hours, For Beginners, Learn Coding Fast: Rust Language, Crash Course Textbook & Exercises (Cookbooks in 8 Hours 15)

BUY & SAVE
$2.99
Rust: Rust Programming, In 8 Hours, For Beginners, Learn Coding Fast: Rust Language, Crash Course Textbook & Exercises (Cookbooks in 8 Hours 15)
5 Learn You Some Erlang for Great Good!: A Beginner's Guide

Learn You Some Erlang for Great Good!: A Beginner's Guide

BUY & SAVE
$41.99
Learn You Some Erlang for Great Good!: A Beginner's Guide
6 Lua: Lua Programming, In 8 Hours, For Beginners, Quick Start Guide (eBook): Lua Crash Course Textbook & Exercises (Textbooks in 8 Hours 7)

Lua: Lua Programming, In 8 Hours, For Beginners, Quick Start Guide (eBook): Lua Crash Course Textbook & Exercises (Textbooks in 8 Hours 7)

BUY & SAVE
$2.99
Lua: Lua Programming, In 8 Hours, For Beginners, Quick Start Guide (eBook): Lua Crash Course Textbook & Exercises (Textbooks in 8 Hours 7)
7 Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

BUY & SAVE
$31.23
Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun
8 Erlang/OTP Volumen I: Un Mundo Concurrente (Spanish Edition)

Erlang/OTP Volumen I: Un Mundo Concurrente (Spanish Edition)

BUY & SAVE
$9.99
Erlang/OTP Volumen I: Un Mundo Concurrente (Spanish Edition)
9 プログラミングErlang (Japanese Edition)

プログラミングErlang (Japanese Edition)

BUY & SAVE
$30.05
プログラミングErlang (Japanese Edition)
+
ONE MORE?

In Erlang, the os:timestamp/0 function can be used to get the current time with microsecond precision. The os:timestamp/0 function returns a tuple in the format {MegaSecs, Secs, MicroSecs}. By extracting the third element (MicroSecs) from the tuple, you can obtain the current time in microseconds.

Here's an example code snippet demonstrating how to retrieve the current time in microseconds using os:timestamp/0:

Now = os:timestamp(), MicroSecs = element(3, Now).

In the above code, the variable Now is assigned the output of os:timestamp/0, which is a tuple representing the current time. Then, using the element/2 function, the third element MicroSecs is extracted from the tuple, which represents the current time in microseconds.

Please note that os:timestamp/0 retrieves the system wall clock time, and the precision could vary depending on the underlying operating system.

How to perform arithmetic operations with the now() value in Erlang?

To perform arithmetic operations with the now() value in Erlang, you can convert it to a more usable format (such as milliseconds) using the erlang:now_to_ms/1 function. After converting it, you can perform arithmetic operations as needed.

Here is an example of how to perform addition and subtraction operations with the now() value in Erlang:

% Get the current time using now() {MegaSecs, Secs, MicroSecs} = now(),

% Convert now() to milliseconds using erlang:now_to_ms/1 CurrentTime = erlang:now_to_ms({MegaSecs, Secs, MicroSecs}),

% Perform addition operation with 10 seconds NewTime = CurrentTime + 10000,

% Perform subtraction operation with 5 seconds PrevTime = CurrentTime - 5000,

% Convert back to the {MegaSecs, Secs, MicroSecs} format {NewMegaSecs, NewSecs, NewMicroSecs} = erlang:timestamp() + NewTime, {PrevMegaSecs, PrevSecs, PrevMicroSecs} = erlang:timestamp() - PrevTime,

% Print the results io:format("Current Time: ~p~n", [erlang:timestamp()]), io:format("New Time: ~p~n", [{NewMegaSecs, NewSecs, NewMicroSecs}]), io:format("Previous Time: ~p~n", [{PrevMegaSecs, PrevSecs, PrevMicroSecs}]).

In this example, now() is first converted to milliseconds using erlang:now_to_ms/1. Then, addition and subtraction operations are performed with the converted time value (in milliseconds). Finally, the results are converted back to the {MegaSecs, Secs, MicroSecs} format for printing.

Note: The now() function in Erlang returns the current time in {MegaSecs, Secs, MicroSecs} format, where MegaSecs is a large integer representing the number of seconds since 1970, Secs is the number of seconds within the current minute, and MicroSecs is the number of microseconds within the current second.

How to compare two timestamps returned by now() in Erlang?

To compare two timestamps returned by os:timestamp/0 or os:system_time/0 in Erlang, you can use the erlang:compare/2 function. Here's an example of how to do it:

{Timestamp1, _} = os:system_time(), {Timestamp2, _} = os:system_time(),

Comparison = erlang:compare(Timestamp1, Timestamp2), case Comparison of less -> io:format("Timestamp1 is less than Timestamp2.~n"); equal -> io:format("Timestamp1 is equal to Timestamp2.~n"); greater -> io:format("Timestamp1 is greater than Timestamp2.~n") end.

In this example, os:system_time/0 is used to retrieve the current timestamp in Erlang. Then, the erlang:compare/2 function is used to compare the two timestamps. The result of the comparison is stored in the variable Comparison, and a case statement is used to print the appropriate message depending on the comparison result.

What is the precision of the timestamp returned by now() in Erlang?

The precision of the timestamp returned by now() in Erlang is approximately one microsecond (1/1000000 seconds).

How to track the execution time of a specific code block using now() in Erlang?

To track the execution time of a specific code block using now() in Erlang, you can follow these steps:

  1. Call now() before executing the code block to capture the start time.
  2. Execute the code block you want to measure the execution time of.
  3. Call now() again after executing the code block to capture the end time.
  4. Calculate the difference between the start and end time to get the execution time.

Here's an example:

% Start time StartTime = now(),

% Code block to measure execution time io:format("Executing code block...~n"), % Your code here...

% End time EndTime = now(),

% Calculate execution time ExecutionTime = timer:now_diff(EndTime, StartTime),

% Print execution time io:format("Execution time: ~p microseconds.~n", [ExecutionTime]).

In this example, now() is used to capture the start and end time, and timer:now_diff/2 is used to calculate the execution time in microseconds. The execution time is then printed using io:format/2.

How to measure the execution time of a function using now() in Erlang?

To measure the execution time of a function using now() in Erlang, you can follow these steps:

  1. First, import the now/0 function from the erlang module. You can do this by adding the following line at the top of your module:

-include_lib("kernel/include/timer.hrl").

  1. Before calling the function whose execution time you want to measure, store the current timestamp using the now() function. For example:

StartTimestamp = now(),

  1. After the function has executed, store the current timestamp again using the now() function. For example:

EndTimestamp = now(),

  1. Calculate the difference between the start and end timestamps using the timer:subtract/2 function. This will give you the execution time in microseconds. For example:

ExecutionTime = timer:subtract(EndTimestamp, StartTimestamp),

  1. You can convert the execution time to another unit (e.g., milliseconds) if desired. For example, to convert it to milliseconds, divide the value by 1000:

ExecutionTimeMs = ExecutionTime / 1000,

  1. The final ExecutionTimeMs variable will now hold the execution time of the function in milliseconds. You can use it for further processing or printing the result.