#!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); sub IfBlocks { my ($test) = @_; if ($test < 50) { die "Test is wrong"; } return $test; } sub UnlessBlocks { my ($test) = @_; unless ($test > 50) { die "Test is wrong"; } return $test; } sub IfStatement { my ($test) = @_; die "Test is wrong" if ($test < 50); return $test; } sub UnlessStatement { my ($test) = @_; die "Test is wrong" unless ($test > 50); return $test; } sub Assert { my ($test) = @_; ($test < 50) || die "Test is wrong"; return $test; } sub Assert2 { my ($test) = @_; ($test > 50) && die "Test is wrong"; return $test; } my @nums = map { ((rand() * 100) % 50) } (0 .. 50); cmpthese(10000, { 'IfBlocks' => sub { eval { IfBlocks($_) } for (@nums) }, 'UnlessBlocks' => sub { eval { UnlessBlocks($_) } for (@nums) }, 'IfStatement' => sub { eval { IfStatement($_) } for (@nums) }, 'UnlessStatement' => sub { eval { UnlessStatement($_) } for (@nums) }, 'Assert' => sub { eval { Assert($_) } for (@nums) }, 'Assert2' => sub { eval { Assert2($_) } for (@nums) }, });