Acme-Time-Constant
view release on metacpan or search on metacpan
lib/Acme/Time/Constant.pm view on Meta::CPAN
This code contains within it, the golden calf of computer science: The ability to execute any code in constant time.
use Acme::Time::Constant qw( constant_time );
my $timestamp = time;
constant_time( '1' => sub {
for ( 0 .. 10 ) { }
} );
print $timestamp - time; # 1 second
constant_time( '1' => sub {
for ( 0 .. 10_000_000 ) { }
} );
print $timestamp - time; # 2 seconds
=head1 BUGS
=head4 General relativity does not hold in the context of this code.
As such, observed time may differ greatly from the time relative to the execution of this code.
B<Workaround:> Get closer to the speed of light.
=head4 Measurements may not be exactly constant.
But Big O is OK with this. We care not that X takes 1.1 seconds and Y takes 1.2 seconds, as long as the variation
is not subject to the size of Y or X.
Random variation between 0.5 and 1.5 seconds is thus within the range of "constant".
B<Workaround:> Imbibe a minimum of 1 L<< Litre|https://en.wikipedia.org/wiki/Litre >> of your favourite neurotoxic substance
before attempting to code.
=head4 Time::HiRes cannot be trusted.
C<Time::HiRes> is under the influence of the Illuminati and as such is part of a conspiracy to prevent us from experiencing
supernormal time.
Using C<Time::HiRes> in your code will subsequently give the illusion that the code no longer executes in constant time.
lib/Acme/Time/Constant.pm view on Meta::CPAN
knowledge of.
One may note that this module depends on C<Time::HiRes>, but this is simply our devilish tactic to make the conspirators
think we're willingly playing for them. You know better. ;)
=head1 FUNCTIONS
=head2 C<constant_time>
constant_time( $time, $callback );
constant_time( $callback ); # $time defaults to 1 second.
=head1 AUTHOR
Kent Fredric <kentnl@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
This is free software; you can redistribute it and/or modify it under
t/00-report-prereqs.t view on Meta::CPAN
$full_prereqs = _merge_prereqs($full_prereqs, $meta->prereqs);
}
else {
$source = 'static metadata';
}
my @full_reports;
my @dep_errors;
my $req_hash = $HAS_CPAN_META ? $full_prereqs->as_string_hash : $full_prereqs;
# Add static includes into a fake section
for my $mod (@include) {
$req_hash->{other}{modules}{$mod} = 0;
}
for my $phase ( qw(configure build test runtime develop other) ) {
next unless $req_hash->{$phase};
next if ($phase eq 'develop' and not $ENV{AUTHOR_TESTING});
for my $type ( qw(requires recommends suggests conflicts modules) ) {
next unless $req_hash->{$phase}{$type};
my $timestamp = time;
constant_time(
1 => sub {
my $j;
for my $i ( 0 .. 1 ) {
$j += $i;
}
}
);
my $delta = time - $timestamp;
cmp_ok( $delta, '>', 0.5, 'Constant time fixed at at least 0.5 second pass 1' );
cmp_ok( $delta, '<', 1.5, 'Constant time fixed at at most 1.5 second pass 1' );
$timestamp = time;
constant_time(
1 => sub {
my $j;
for my $i ( 0 .. 2_000_000 ) {
$j += $i;
}
}
);
$delta = time - $timestamp;
cmp_ok( $delta, '>', 0.5, 'Constant time fixed at at least 0.5 second pass 2' );
cmp_ok( $delta, '<', 1.5, 'Constant time fixed at at most 1.5 second pass 2' );
done_testing;
my $timestamp = time;
constant_time(
sub {
my $j;
for my $i ( 0 .. 1 ) {
$j += $i;
}
}
);
my $delta = time - $timestamp;
cmp_ok( $delta, '>', 0.5, 'Constant time fixed at at least 0.5 second pass 1' );
cmp_ok( $delta, '<', 1.5, 'Constant time fixed at at most 1.5 second pass 1' );
done_testing;
( run in 0.870 second using v1.01-cache-2.11-cpan-39bf76dae61 )