Skip to content

Instantly share code, notes, and snippets.

@andrewsolomon
Created February 1, 2015 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewsolomon/3c0a1e0b998ab9d911b8 to your computer and use it in GitHub Desktop.
Save andrewsolomon/3c0a1e0b998ab9d911b8 to your computer and use it in GitHub Desktop.
Speed of Boolean equality operators
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes;
my @tfs = map { $_ % 2 } (1 .. 10000);
# Just warming up the arrays
my $z;
foreach my $x (@tfs) {
foreach my $y (@tfs) {
$z = $x + $y;
}
}
my $start;
my $end;
$start = Time::HiRes::gettimeofday();
foreach my $x (@tfs) {
foreach my $y (@tfs) {
$z = !($x xor $y);
}
}
$end = Time::HiRes::gettimeofday();
printf("Exclusivity %.2f\n", $end - $start);
$start = Time::HiRes::gettimeofday();
foreach my $x (@tfs) {
foreach my $y (@tfs) {
$z = !$x == !$y;
}
}
$end = Time::HiRes::gettimeofday();
printf("Naysayers %.2f\n", $end - $start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment