{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ecfe2918",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import math\n",
    "import time\n",
    "from mxnet import np\n",
    "from d2l import mxnet as d2l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e70e2481",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 10000\n",
    "a = np.ones(n)\n",
    "b = np.ones(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1451402f",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Timer: #@save\n",
    "    \"\"\"Record multiple running times.\"\"\"\n",
    "    def __init__(self):\n",
    "        self.times = []\n",
    "        self.start()\n",
    "        \n",
    "    def start(self):\n",
    "        \"\"\"Start the timer.\"\"\"\n",
    "        self.tik = time.time()\n",
    "        \n",
    "    def stop(self):\n",
    "        \"\"\"Stop the timer and record the time in a list.\"\"\"\n",
    "        self.times.append(time.time() - self.tik)\n",
    "        return self.times[-1]\n",
    "    \n",
    "    def avg(self):\n",
    "        \"\"\"Return the average time.\"\"\"\n",
    "        return sum(self.times) / len(self.times)\n",
    "    \n",
    "    def sum(self):\n",
    "        \"\"\"Return the sum of time.\"\"\"\n",
    "        return sum(self.times)\n",
    "    \n",
    "    def cumsum(self):\n",
    "        \"\"\"Return the accumulated time.\"\"\"\n",
    "        return np.array(self.times).cumsum().tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cdde28c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.95554 sec'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Running time of Pythin loop \n",
    "\n",
    "c = np.zeros(n)\n",
    "\n",
    "timer = Timer()              # Start timer\n",
    "\n",
    "for i in range(n):\n",
    "    c[i] = a[i] + b[i]\n",
    "    \n",
    "f'{timer.stop():.5f} sec'    # Print elasped time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a49a0366",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.00056 sec'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Running time of vectorized addition\n",
    "\n",
    "timer.start()\n",
    "\n",
    "d = a + b\n",
    "\n",
    "f'{timer.stop():.5f} sec'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d733fd0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
