-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Original (int64): 7.629520416259766 MB\n", | ||
"Int16: 2.8611488342285156 MB\n", | ||
"Int32: 4.768497467041016 MB\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"# 100만 행의 연도 데이터 생성\n", | ||
"years = np.random.randint(1900, 2025, size=1_000_000)\n", | ||
"df = pd.DataFrame({'year': years})\n", | ||
"\n", | ||
"# 메모리 사용량 비교\n", | ||
"print(\"Original (int64):\", df['year'].memory_usage(deep=True) / 1024 / 1024, \"MB\")\n", | ||
"\n", | ||
"df['year_int16'] = df['year'].astype('Int16')\n", | ||
"print(\"Int16:\", df['year_int16'].memory_usage(deep=True) / 1024 / 1024, \"MB\")\n", | ||
"\n", | ||
"df['year_int32'] = df['year'].astype('Int32')\n", | ||
"print(\"Int32:\", df['year_int32'].memory_usage(deep=True) / 1024 / 1024, \"MB\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
"1. 초기 상태 (컬럼별):\n", | ||
"text_before: object, 55.313236236572266 MB\n", | ||
"float_before: float64, 7.629520416259766 MB\n", | ||
"year: int64, 7.629520416259766 MB\n", | ||
"float_after: float64, 7.629520416259766 MB\n", | ||
"text_after: object, 56.266910552978516 MB\n", | ||
"Total: 134.46820449829102 MB\n", | ||
"\n", | ||
"2. year를 Int16으로 변경 후 (컬럼별):\n", | ||
"text_before: object, 55.313236236572266 MB\n", | ||
"float_before: float64, 7.629520416259766 MB\n", | ||
"year: Int16, 2.8611488342285156 MB\n", | ||
"float_after: float64, 7.629520416259766 MB\n", | ||
"text_after: object, 56.266910552978516 MB\n", | ||
"Total: 129.69983291625977 MB\n", | ||
"\n", | ||
"3. year를 Int32로 변경 후 (컬럼별):\n", | ||
"text_before: object, 55.313236236572266 MB\n", | ||
"float_before: float64, 7.629520416259766 MB\n", | ||
"year: Int32, 4.768497467041016 MB\n", | ||
"float_after: float64, 7.629520416259766 MB\n", | ||
"text_after: object, 56.266910552978516 MB\n", | ||
"Total: 131.60718154907227 MB\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"# 다양한 타입의 컬럼들 사이에 연도 데이터 배치\n", | ||
"df = pd.DataFrame({\n", | ||
" 'text_before': ['some_text'] * 1_000_000, \n", | ||
" 'float_before': np.random.random(1_000_000), \n", | ||
" 'year': np.random.randint(1900, 2025, size=1_000_000),\n", | ||
" 'float_after': np.random.random(1_000_000), \n", | ||
" 'text_after': ['other_text'] * 1_000_000 \n", | ||
"})\n", | ||
"\n", | ||
"# 초기 상태\n", | ||
"print(\"\\n1. 초기 상태 (컬럼별):\")\n", | ||
"for col in df.columns:\n", | ||
" print(f\"{col}: {df[col].dtype},\", df[col].memory_usage(deep=True) / 1024 / 1024, \"MB\")\n", | ||
"print(\"Total:\", df.memory_usage(deep=True).sum() / 1024 / 1024, \"MB\")\n", | ||
"\n", | ||
"# year를 Int16으로 변경\n", | ||
"df['year'] = df['year'].astype('Int16')\n", | ||
"print(\"\\n2. year를 Int16으로 변경 후 (컬럼별):\")\n", | ||
"for col in df.columns:\n", | ||
" print(f\"{col}: {df[col].dtype},\", df[col].memory_usage(deep=True) / 1024 / 1024, \"MB\")\n", | ||
"print(\"Total:\", df.memory_usage(deep=True).sum() / 1024 / 1024, \"MB\")\n", | ||
"\n", | ||
"# year를 Int32로 변경\n", | ||
"df['year'] = df['year'].astype('Int32')\n", | ||
"print(\"\\n3. year를 Int32로 변경 후 (컬럼별):\")\n", | ||
"for col in df.columns:\n", | ||
" print(f\"{col}: {df[col].dtype},\", df[col].memory_usage(deep=True) / 1024 / 1024, \"MB\")\n", | ||
"print(\"Total:\", df.memory_usage(deep=True).sum() / 1024 / 1024, \"MB\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".venv", | ||
"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.12.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
from multiprocessing import Pool | ||
import time | ||
from multiprocessing import Pool | ||
|
||
|
||
def slow_square(x): | ||
time.sleep(1) # 작업이 오래 걸리는 것을 시뮬레이션 | ||
return x * x | ||
|
||
|
||
def main(): | ||
numbers = [1, 2, 3, 4, 5] | ||
|
||
print("일반적인 처리 시작") | ||
start = time.time() | ||
regular_result = [slow_square(x) for x in numbers] | ||
print(f"일반 처리 결과: {regular_result}") | ||
print(f"소요 시간: {time.time() - start:.2f}초\n") | ||
|
||
print("병렬 처리 시작") | ||
start = time.time() | ||
with Pool() as pool: | ||
parallel_result = pool.map(slow_square, numbers) | ||
print(f"병렬 처리 결과: {list(parallel_result)}") | ||
print(f"소요 시간: {time.time() - start:.2f}초") | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
"source": [ | ||
"from files import pool_test\n", | ||
"\n", | ||
"\n", | ||
"pool_test.main()" | ||
] | ||
} | ||
|