{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Cost of operations on lists</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inserting elements at the end of a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from time import time\n",
    "\n",
    "data = []\n",
    "for i in range(1000, 50001, 1000):\n",
    "    L = []\n",
    "    before = time()\n",
    "    for _ in range(i):\n",
    "        L.append(None)\n",
    "    after = time()\n",
    "    data.append((i, after - before))\n",
    "plot(*tuple(zip(*data)))\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inserting elements at the beginning of a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from time import time\n",
    "\n",
    "data = []\n",
    "for i in range(1000, 50001, 1000):\n",
    "    L = []\n",
    "    before = time()\n",
    "    for _ in range(i):\n",
    "        L.insert(0, None)\n",
    "    after = time()\n",
    "    data.append((i, after - before))\n",
    "plot(*tuple(zip(*data)))\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Size of a list initialised to a given number of elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from sys import getsizeof\n",
    "\n",
    "data = []\n",
    "for i in range(1, 51):\n",
    "    data.append((i, getsizeof([None] * i)))\n",
    "plot(*tuple(zip(*data)))\n",
    "print()   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Size of a list to which elements are appended incrementally"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from sys import getsizeof\n",
    "\n",
    "data = []\n",
    "L = []\n",
    "for i in range(1, 51):\n",
    "    L.append(None)\n",
    "    data.append((i, getsizeof(L)))\n",
    "plot(*tuple(zip(*data)))\n",
    "print()   "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

Resource created Wednesday 12 August 2015, 09:53:14 AM.

file: cost_of_operations_on_lists.ipynb


Back to top

COMP9021 15s2 (Principles of Programming) is powered by WebCMS3
CRICOS Provider No. 00098G