{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measure execution time\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```perl\n",
    "> use Benchmark qw(:all);\n",
    "> $r=rand_sphere(3,1000,seed=>1); $t=timeit(1,'$r->FACETS;'); print timestr($t), \"\\n\";\n",
    "> $r=rand_sphere(3,1000,seed=>1); $t=timeit(1,'prefer_now \"beneath_beyond\";$r->FACETS;'); print timestr($t);\n",
    "```\n",
    "```\n",
    "1 wallclock secs ( 1.52 usr +  0.00 sys =  1.52 CPU) @  0.66/s (n=1)\n",
    "1 wallclock secs ( 0.38 usr +  0.00 sys =  0.38 CPU) @  2.63/s (n=1)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that if timing a user function, you have to provide the application your function lives in:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```perl\n",
    "> $t=timeit(1,'Polymake::polytope::rand_box(10,2000,1);'); print timestr($t);\n",
    "```\n",
    "```\n",
    "0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU) @ 100.00/s (n=1)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above code does not work in a [script](https://polymake.org/doku.php/user_guide/howto/scripting) file (.pl) because of polymake's modifications to Perl. You rather want to use something like this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "use Benchmark qw(:all);\n",
    "use application 'polytope';\n",
    "    \n",
    "my $r=rand_sphere(3,100,seed=>1);\n",
    "    \n",
    "sub getfacets{\n",
    "  $r->FACETS;\n",
    "}\n",
    "   \n",
    "sub myBenchmark{\n",
    "  my $t=Benchmark::timeit(1,\"getfacets\"); \n",
    "  print timestr($t);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       " 0 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU) @ 50.00/s (n=1)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<details><summary><pre style=\"display:inline\"><small>Click here for additional output</small></pre></summary>\n",
       "<pre>\n",
       "polymake: used package lrs\n",
       "  Implementation of the reverse search algorithm of Avis and Fukuda.\n",
       "  Copyright by David Avis.\n",
       "  http://cgm.cs.mcgill.ca/~avis/C/lrs.html\n",
       "\n",
       "</pre>\n",
       "</details>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "myBenchmark;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "use Benchmark qw(:all);\n",
    "use application 'polytope';\n",
    "    \n",
    "sub myBenchmark2($$) {\n",
    "    my ($d,$n)=@_;\n",
    "    my $r=rand_sphere($d,$n,seed=>1);\n",
    "    \n",
    "    my $t0= Benchmark->new;\n",
    "    $r->FACETS;\n",
    "    my $t1=Benchmark->new;\n",
    "    my $td1=timediff($t1,$t0);\n",
    "    print \"FACETS: \".timestr($td1).\"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FACETS:  1 wallclock secs ( 1.46 usr +  0.00 sys =  1.46 CPU)\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myBenchmark2(3,1000);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pl",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
