๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Langauge/Python

[Python] ์—ฐ์‚ฐ ์†๋„ ์˜ฌ๋ฆฌ๊ธฐ - ๊ฐœ์š” ๋ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ

728x90
๋ฐ˜์‘ํ˜•
๐Ÿ ๋ณธ ๊ฒŒ์‹œ๊ธ€์€ Python 3.11.0 ํ™˜๊ฒฝ์—์„œ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!

 

ํŒŒ์ด์ฌ์€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด๋กœ ์•Œ๋ ค์ ธ ์žˆ์ง€๋งŒ ์‚ฌ์‹ค์€ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์–ธ์–ด๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ ์ด์œ ๋Š” ๋™์ž‘ ๋ฐฉ์‹์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

ํŒŒ์ด์ฌ ์ฝ”๋“œ๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์„ ๋ณด๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ Python Source Code๋ฅผ Python Interpreter์— ์ „๋‹ฌํ•˜๋ฉด ๋‚ด๋ถ€์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์ด ์ง„ํ–‰๋œ๋‹ค.

  • Python Complier๊ฐ€ ํŒŒ์ด์ฌ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ๋ฌธ๋ฒ• ๊ฒ€์‚ฌ ํ›„ ์˜ค๋ฅ˜๊ฐ€ ์—†์œผ๋ฉด ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ ํ•œ๋‹ค.
  • PVM(Python Virtual Machine)์ด ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ๊ธฐ๊ณ„์–ด๋กœ ํ•œ์ค„์”ฉ ๋ฒˆ์—ญํ•œ๋‹ค.

 

์ด๋Ÿฐ ๊ณผ์ •์ด ๋๋‚œ ํ›„ ์ปดํ“จํ„ฐ๋Š” ๋ฒˆ์—ญ๋œ ๊ธฐ๊ณ„์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

Python Compiler๊ฐ€ ๋ณ€ํ™˜ํ•œ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ํ”„๋กœ์ ํŠธ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ __pycache__๋ผ๋Š” ํด๋”์˜ .pyc์ด ์ปดํŒŒ์ผ๋œ ๋ฐ”์ดํŠธ ์ฝ”๋“œ ์ด๋‹ค.

 

 

ํŒŒ์ด์ฌ์€ ์ด๋ ‡๋“ฏ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ธฐ๊ณ„์–ด๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์— C,C++, ์–ด์…ˆํ”Œ๋ฆฌ์–ด ๊ฐ™์€ ์ €๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ณด๋‹ค๋Š” ๋Š๋ฆด ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

 

ํ•˜์ง€๋งŒ ํŒŒ์ด์ฌ์„ ์ฝ”๋”ฉ์„ ํ•˜๋‹ค๋ณด๋ฉด ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ํ•„์š”ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค์„ ํ†ตํ•ด ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์‹œ๋„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ: ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ ์ ˆํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฆฌ์ŠคํŠธ ๋Œ€์‹  ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต ์ œ๊ฑฐํ•  ๋•Œ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋œ๋‹ค.
  • ๋‚ด์žฅ ํ•จ์ˆ˜์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ™œ์šฉ: Python์˜ ๋‚ด์žฅ ํ•จ์ˆ˜๋‚˜ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(์˜ˆ: NumPy, Pandas)๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ํ–ฅ์ƒ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ์ฝ”๋“œ ํ”„๋กœํŒŒ์ผ๋ง: cProfile๊ณผ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ์˜ ์„ฑ๋Šฅ ๋ณ‘๋ชฉ ์ง€์ ์„ ์ฐพ์•„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ: ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋”ฉ์ด๋‚˜ ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์‹ฑ์„ ํ†ตํ•ด CPU์˜ ์—ฌ๋Ÿฌ ์ฝ”์–ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • JIT ์ปดํŒŒ์ผ๋Ÿฌ ์‚ฌ์šฉ: PyPy์™€ ๊ฐ™์€ JIT(JIT Compilation) ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹คํ–‰ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

 

์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ ๊ฐœ์„ 

์ ์ ˆํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Python ์ฝ”๋“œ์˜ ์‹คํ–‰ ์†๋„๋ฅผ ๋†’ํž ์ˆ˜ ์žˆ๋‹ค.

๋Œ€ํ‘œ์ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ, ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด append ํ•˜๋Š” ๋ฐฉ๋ฒ•๋ณด๋‹ค ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜(List Comprehension) ๋ฐฉ๋ฒ•์œผ๋กœ ํ•˜๋ฉด ๋” ๋น ๋ฅด๋‹ค.

import time
if __name__ == "__main__":
    start = time.time()
    a = []
    for i in range(100000000):
        a.append(i)
    end = time.time()
    print(f"Default : {end-start:.4f}sec")

    start = time.time()
    a = [i for i in range(100000000)]
    end = time.time()
    print(f"List Comprehension : {end-start:.4f}sec")

 

 

์ด๋ ‡๊ฒŒ ์†๋„์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ์ด์œ ๋Š” ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์ตœ์ ํ™”๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

์ด๋ ‡๊ฒŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•  ๋•Œ๋Š” ๋ฆฌ์ŠคํŠธ(ํŠœํ”Œ, ์ง‘ํ•ฉ, ์‚ฌ์ „) ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

 

์ถ”๊ฐ€๋กœ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณตํ•  ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€ ์ด๋‹ค.

import time
if __name__ == "__main__":
    start = time.time()
    a = 0
    for i in range(1000000):
        a+=i
    print(a)
    end = time.time()
    print(f"Default : {end-start:.4f}sec")

    start = time.time()
    a = list(range(1000000))
    a = sum(a)
    print(a)
    end = time.time()
    print(f"SUM : {end-start:.4f}sec")

 

sum์ด ๋” ๋น ๋ฅธ ์ด์œ ๋„ C๋กœ ์ตœ์ ํ™”๋œ ๋‚ด๋ถ€ ๊ตฌํ˜„์ด ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

ํŠน์ • ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์–ด๋–ค ๊ฒƒ์„ ์„ ํƒํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ์„ฑ๋Šฅ์ด ๋‹ฌ๋ผ์ง„๋‹ค.

๋‹ค์Œ์€ ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•ด A -> N๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๋‘˜๋‹ค ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ๋ชฉํ‘œ ๋…ธ๋“œ๊ฐ€ ๊นŠ์€ ๊ณณ์— ์œ„์น˜ํ•˜๋ฏ€๋กœ DFS ์ข€ ๋” ๋น ๋ฅด๋‹ค. (๋น„๊ต๋ฅผ ์œ„ํ•ด ์ธ์œ„์ ์œผ๋กœ ๋™์ผํ•œ ๋Œ€๊ธฐ์‹œ๊ฐ„ ๋ถ€์—ฌ)

import time
from collections import deque

# BFS ๊ตฌํ˜„
def bfs(graph, start, goal):
    visited = set()
    queue = deque([(start, [start])])  # (ํ˜„์žฌ ๋…ธ๋“œ, ๊ฒฝ๋กœ)
    
    while queue:
        current_node, path = queue.popleft()
        time.sleep(0.1)
        if current_node == goal:
            return path
        
        for neighbor in graph[current_node]:
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append((neighbor, path + [neighbor]))
    
    return None  # ๊ฒฝ๋กœ๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ

# DFS ๊ตฌํ˜„
def dfs(graph, start, goal, path=None):
    if path is None:
        path = []
    path.append(start)
    time.sleep(0.1)
    if start == goal:
        return path
    
    for neighbor in graph[start]:
        if neighbor not in path:
            result = dfs(graph, neighbor, goal, path)
            if result is not None:
                return result
    
    path.pop()
    return None

if __name__ =="__main__":
    graph = {
        'A': ['B', 'C', 'D'],
        'B': ['A', 'E', 'F'],
        'C': ['A', 'G', 'H'],
        'D': ['A', 'I', 'J'],
        'E': ['B', 'K', 'L'],
        'F': ['B'],
        'G': ['C'],
        'H': ['C', 'M', 'N'],
        'I': ['D'],
        'J': ['D'],
        'K': ['E'],
        'L': ['E'],
        'M': ['H'],
        'N': ['H'],
    }

    # ๋” ๋งŽ์€ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    for i in range(15, 30):
        graph[chr(65 + i)] = [chr(65 + i - 1), chr(65 + i - 2)]  # A-Z๊นŒ์ง€ ๋…ธ๋“œ ์ถ”๊ฐ€

    # ๊ฒฝ๋กœ ์ฐพ๊ธฐ์™€ ์‹œ๊ฐ„ ์ธก์ •
    start_node = 'A'
    goal_node = 'N'

    # BFS ์‹œ๊ฐ„ ์ธก์ •
    start_time_bfs = time.time()
    bfs_path = bfs(graph, start_node, goal_node)
    end_time_bfs = time.time()
    bfs_time = end_time_bfs - start_time_bfs

    # DFS ์‹œ๊ฐ„ ์ธก์ •
    start_time_dfs = time.time()
    dfs_path = dfs(graph, start_node, goal_node)
    end_time_dfs = time.time()
    dfs_time = end_time_dfs - start_time_dfs

    # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    print("BFS๋กœ ์ฐพ์€ A์—์„œ N๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ:", bfs_path)
    print(f"BFS ์‹คํ–‰ ์‹œ๊ฐ„:{bfs_time:.7f}")

    print("DFS๋กœ ์ฐพ์€ A์—์„œ N๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ:", dfs_path)
    print(f"DFS ์‹คํ–‰ ์‹œ๊ฐ„:{dfs_time:.7f}")

 

728x90
๋ฐ˜์‘ํ˜•