{ "cells": [ { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:50:41.338639Z", "start_time": "2022-08-08T14:50:41.336210Z" } }, "outputs": [], "source": [ "import random\n", "import time\n", "from copy import copy, deepcopy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:38:39.674168Z", "start_time": "2022-08-08T14:37:49.304723Z" } }, "outputs": [], "source": [ "n = 1000000\n", "data = [random.randint(10000, 1000000) for _ in range(n)]\n", "map = {}\n", "for i in range(n):\n", " map[i] = [random.randint(10, 100) for _ in range(random.randint(1, 100))]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:39:13.561799Z", "start_time": "2022-08-08T14:39:13.559024Z" } }, "outputs": [], "source": [ "class A:\n", " def __init__(self, data, map):\n", " self.data = data\n", " self.map = map" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:41:12.516036Z", "start_time": "2022-08-08T14:40:43.597542Z" } }, "outputs": [], "source": [ "aa = A(data, map)\n", "aa_copy = deepcopy(aa)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:40:13.291495Z", "start_time": "2022-08-08T14:40:13.286752Z" } }, "outputs": [], "source": [ "_dispatcher = {}\n", "\n", "def _copy_list(_l):\n", " ret = _l.copy()\n", " for idx, item in enumerate(ret):\n", " cp = _dispatcher.get(type(item))\n", " if cp is not None:\n", " ret[idx] = cp(item)\n", " return ret\n", "_dispatcher[list] = _copy_list\n", "\n", "def _copy_dict(d):\n", " ret = d.copy()\n", " for key, value in ret.items():\n", " cp = _dispatcher.get(type(value))\n", " if cp is not None:\n", " ret[key] = cp(value)\n", "\n", " return ret\n", "_dispatcher[dict] = _copy_dict\n", "\n", "def mydeepcopy(sth):\n", " cp = _dispatcher.get(type(sth))\n", " if cp is None:\n", " return sth\n", " else:\n", " return cp(sth)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:40:02.022132Z", "start_time": "2022-08-08T14:40:02.018642Z" } }, "outputs": [], "source": [ "class B:\n", " def __init__(self, data, map):\n", " self.data = data\n", " self.map = map\n", " def __deepcopy__(self, memodict={}):\n", " print('called')\n", " data = mydeepcopy(self.data)\n", " map = mydeepcopy(self.map)\n", " return B(data, map)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:41:30.125066Z", "start_time": "2022-08-08T14:41:30.122706Z" } }, "outputs": [], "source": [ "bb = B(data, map)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2022-08-08T14:41:42.335403Z", "start_time": "2022-08-08T14:41:31.435029Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "called\n" ] } ], "source": [ "bb_copy = deepcopy(bb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }