{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import multiprocessing\n", "import time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 멀티쓰레드 미사용시 시간" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p1 : 10000000\n", "p1 : 20000000\n", "p1 : 30000000\n", "p2 : 10000000\n", "p2 : 20000000\n", "p2 : 30000000\n", "p3 : 10000000\n", "p3 : 20000000\n", "p3 : 30000000\n", "p4 : 10000000\n", "p4 : 20000000\n", "p4 : 30000000\n", "====8.327701091766357 seconds====\n", "CPU times: user 8.3 s, sys: 21.3 ms, total: 8.32 s\n", "Wall time: 8.33 s\n" ] } ], "source": [ "%%time\n", "start_time = time.time()\n", "\n", "def count(name):\n", " for i in range(1, 30000001):\n", " if (i%10000000)==0:\n", " print(name, \" : \", i)\n", " \n", "num_list = ['p1','p2','p3','p4']\n", "\n", "for num in num_list:\n", " count(num)\n", " \n", "print(\"===={} seconds====\".format(time.time()-start_time))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p2 : 10000000\n", "p3 : 10000000\n", "p4 : 10000000\n", "p1 : 10000000\n", "p2 : 20000000\n", "p3 : 20000000\n", "p4 : 20000000\n", "p1 : 20000000\n", "p2 : 30000000\n", "p4 : 30000000\n", "p3 : 30000000\n", "p1 : 30000000\n", "====2.744521141052246 seconds====\n", "CPU times: user 17.5 ms, sys: 20.1 ms, total: 37.5 ms\n", "Wall time: 2.75 s\n" ] } ], "source": [ "%%time\n", "start_time = time.time()\n", "\n", "def count(name):\n", " for i in range(1, 30000001):\n", " if (i%10000000)==0:\n", " print(name, \" : \", i)\n", " \n", "num_list = ['p1','p2','p3','p4']\n", "\n", "pool = multiprocessing.Pool(processes=4)\n", "pool.map(count, num_list)\n", "pool.close()\n", "pool.join()\n", " \n", "print(\"===={} seconds====\".format(time.time()-start_time))" ] }, { "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.5" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }