sub andNeg { return (($_[0]^$_[1]) < 0); } #### print "\n\n"; for (0..3) { my $x = 1 - 2 * ($_ % 2); my $y = 1 - 2 * int($_ / 2); print "\n"; } print "
", join("", "x", "y", "andNeg()", "spaceship()", "mult()", "anchor()"), "
", join("", $x, $y, andNeg($x, $y), spaceship($x, $y), mult($x, $y), anchor($x, $y)), "
\n"; ##
## sub spaceship { (0 <=> $_[0]) == (0<=> $_[1]); } sub mult { $_[0]*$_[1] > 0; } sub anchor { if ($_[0] > 0) { if ($_[1] > 0) { # both pos return 1 } } elsif ($_[0] < 0) { if ($_[1] < 0) { # both neg return 1 } } return 0; } #### #!perl -w # use Time::HiRes qw(time); print "\n\n"; for (0..3) { my $x = 1 - 2 * ($_ % 2); my $y = 1 - 2 * int($_ / 2); print "\n"; } print "
", join("", "x", "y", "andNeg()", "spaceship()", "mult()", "anchor()"), "
", join("", $x, $y, andNeg($x, $y), spaceship($x, $y), mult($x, $y), anchor($x, $y)), "
\n"; my $count = 1000000; for ('unit square', 'signed int') { doTimeTest($count, $_); } sub doTimeTest { my $count = shift; my $type = shift; my @coordinates = primeCoordinates($count, $type ); my $time0 = Time::HiRes::time(); foreach (@coordinates) { spaceship($_->[0],$_->[1]); } my $time1 = Time::HiRes::time(); foreach (@coordinates) { mult($_->[0], $_->[1]); } my $time2 = Time::HiRes::time(); foreach (@coordinates) { anchor($_->[0], $_->[1]); } my $time3 = Time::HiRes::time; my $spaceship = $time1 - $time0; my $multiple = $time2 - $time1; my $logic = $time3 - $time2; print join("\t", $type, "spaceship", "multiply", "logic"), "\n"; print join("\t", "spaceship", '-'x8, pretty($spaceship,$multiple), pretty($spaceship,$logic)), "\n"; print join("\t", "multiply ", pretty($multiple,$spaceship), '-'x8, pretty($multiple,$logic)), "\n"; print join("\t", "logic ", pretty($logic,$spaceship), pretty($logic,$multiple), '-'x8), "\n"; print "\n"; } sub pretty { my ($one, $two) = @_; if ($one > $two) { return sprintf("worse %.2f%%", ($one - $two) / $one * 100); } else { return sprintf("better %.2f%%", ($two - $one) / $two * 100); } } sub spaceship { (0 <=> $_[0]) == (0<=> $_[1]); } sub mult { $_[0]*$_[1] > 0; } sub anchor { if ($_[0] > 0) { if ($_[1] > 0) { # both pos return 1 } } elsif ($_[0] < 0) { if ($_[1] < 0) { # both neg return 1 } } return 0; } sub andNeg { return (($_[0]^$_[1]) < 0); } sub primeCoordinates { my $count = shift; my $type = shift; my @retval; if ($type eq 'unit square') { for (0..$count-1) { $retval[$_] = [ 1 - 2 * rand(), 1 - 2 * rand()]; } } elsif ($type eq 'signed int') { use POSIX; for (0..$count-1) { $retval[$_] = [ rand() > 0.5 ? rand(INT_MAX) : -1 * rand(INT_MAX), rand() > 0.5 ? rand(INT_MAX) : -1 * rand(INT_MAX)]; } } else { die "Blue. No yel-- Auuuuuuuugh!\n"; } return @retval; }