Skip to content

Instantly share code, notes, and snippets.

@viliampucik
Created December 28, 2011 20:32
Show Gist options
  • Save viliampucik/1529594 to your computer and use it in GitHub Desktop.
Save viliampucik/1529594 to your computer and use it in GitHub Desktop.
Benchmark of Empty String Comparisons
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark ( 'cmpthese' );
my $foo = '';
cmpthese( 10000000, {
'direct' => sub { return $foo eq '' },
'length' => sub { return length $foo },
'regexp' => sub { return $foo =~ /^$/ },
});
$foo = 'something';
cmpthese( 10000000, {
'direct' => sub { return $foo eq '' },
'length' => sub { return length $foo },
'regexp' => sub { return $foo =~ /^$/ },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment