#!/usr/bin/perl -lw
use strict;
use Time::HiRes "gettimeofday";
use POSIX "fmod";
use Math::Complex;
my $pi = 3.141592653589793238462643383279502884197169399375105820974;
my $step = $pi / 10000;
sub mycos {
my $x = abs(shift() / $pi);
$x ** $x * Re(cplx(-$x) ** -$x);
}
my ($a, $b, $c, $d);
($a, $b) = gettimeofday;
for (my $i = 0; $i < 2 * $pi; $i += $step) {
my $x = cos($i);
}
($c, $d) = gettimeofday;
my $ta = ($c-$a+($d-$b)/1000000);
print "cos() took: $ta seconds";
($a, $b) = gettimeofday;
for (my $i = 0; $i < 2 * $pi; $i += $step) {
my $c = mycos($i);
}
($c, $d) = gettimeofday;
my $tb = ($c-$a+($d-$b)/1000000);
print "mycos() took: $tb seconds";
print "mycos() takes approximately ".($tb/$ta)." times longer to run than cos()";
print "sanity:";
for (my $i = 0; $i <= 2 * $pi; $i += 1) {
printf "cos (%.15f): \t%+.15f\n", $i, cos($i);
printf "mycos(%.15f): \t%+.15f\n", $i, mycos($i);
}