Prev: Using 3rd party Perl 6 modules

Timestamp and elapsed time in Perl 6

Handling dates and time is tricky. No matter what programming language you use.

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.


Perl 6 provides two functions returning the number of seconds from the epoch (1970 January 1).

The now function returns an Instant object that holds the number of seconds since the epoch with a precision of up to 6 digits after the decimal point.

The time function returns the number of seconds since the epoch less the number of leap seconds. So in January 2015 now return 35 seconds more than time.


use v6;

my $now = now;
my $time = time;

say $now.Int - $time;    # 35

Elapsed time in Perl 6

If you want to measure elapsed time of some code snippet, probably the best is to save the result of the now function at the two points in the execution: before the snippet and after the snippet and then take the difference of the two Instant objects resulting in a Duration object.


use v6;

my $start = now;


my $end = now;

my $elapsed = $end - $start;
say $elapsed.WHAT;  # (Duration)
say $elapsed;       # 0.007443

sub some_call {
	my $x = rand;
	my $y = rand;
	my $z = $x + $y;

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-01-11

Prev: Using 3rd party Perl 6 modules


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.58862704

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.