Skip to content

Instantly share code, notes, and snippets.

@jacoby
Created April 8, 2022 18:10
Show Gist options
  • Save jacoby/bd0e7a4500b251d6d053ef5b9d758747 to your computer and use it in GitHub Desktop.
Save jacoby/bd0e7a4500b251d6d053ef5b9d758747 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw(:all);
my $count = 1_000_000;
timethese(
$count,
{
'ForLoop' => sub {
my @array = 1 .. 1000;
for (@array) {
shift @array;
}
},
'WhileLoop' => sub {
my @array = 1 .. 1000;
for (@array) {
shift @array;
}
},
}
);
./forwhile.pl
Benchmark: timing 1000000 iterations of ForLoop, WhileLoop...
ForLoop: 116 wallclock secs (71.87 usr + 3.64 sys = 75.51 CPU) @ 13243.28/s (n=1000000)
WhileLoop: 134 wallclock secs (74.98 usr + 3.76 sys = 78.74 CPU) @ 12700.03/s (n=1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment