Measure execution time

The benefit of using a standard programming language such as Perl is that one can use standard libraries for basic needs. For example, one can measure the runtime/execution time of a sequence of commands. Here is an example showing how to benchmark two different convex hull algorithms/codes on the same example.

> use Benchmark qw(:all);
> $r=rand_sphere(3,1000,seed=>1); $t=timeit(1,'$r->FACETS;'); print timestr($t), "\n";
> $r=rand_sphere(3,1000,seed=>1); $t=timeit(1,'prefer_now "beneath_beyond";$r->FACETS;'); print timestr($t);
1 wallclock secs ( 1.52 usr +  0.00 sys =  1.52 CPU) @  0.66/s (n=1)
1 wallclock secs ( 0.38 usr +  0.00 sys =  0.38 CPU) @  2.63/s (n=1)

Note that if timing a user function, you have to provide the application your function lives in:

> $t=timeit(1,'Polymake::polytope::rand_box(10,2000,1);'); print timestr($t);
0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU) @ 100.00/s (n=1)

The above code does not work in a script file (.pl) because of polymake's modifications to Perl. You rather want to use something like this.

> use Benchmark qw(:all);
> use application 'polytope';
>     
> my $r=rand_sphere(3,100,seed=>1);
>     
> sub getfacets{
>   $r->FACETS;
> }
>    
> sub myBenchmark{
>   my $t=Benchmark::timeit(1,"getfacets"); 
>   print timestr($t);
> }
> myBenchmark;
 0 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU) @ 50.00/s (n=1)

Or this:

> use Benchmark qw(:all);
> use application 'polytope';
>     
> sub myBenchmark2($$) {
>     my ($d,$n)=@_;
>     my $r=rand_sphere($d,$n,seed=>1);
>     
>     my $t0= Benchmark->new;
>     $r->FACETS;
>     my $t1=Benchmark->new;
>     my $td1=timediff($t1,$t0);
>     print "FACETS: ".timestr($td1)."\n";
> }
> myBenchmark2(3,1000);
FACETS:  1 wallclock secs ( 1.46 usr +  0.00 sys =  1.46 CPU)