728x90
๋ฐ์ํ
๐ ๋ณธ ๊ฒ์๊ธ์ Python 3.11.0 ํ๊ฒฝ์์ ์์ฑ๋์์ต๋๋ค!
์ฐ์ฐ ์๋๋ฅผ ์ฌ๋ฆฌ๊ธฐ ์ํด์ ํ์ด์ฌ์ ๋ด์ฅํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ํ ์ ์๋ค.
์์ : ๋ฆฌ์คํธ ์ค๋ณต ์ ๊ฑฐ ๋ฐ ์ ๋ ฌ
import time
if __name__ == "__main__":
# ์ค๋ณต๋ ์ซ์๊ฐ ํฌํจ๋ ๋ฆฌ์คํธ
numbers = [5, 3, 8, 5, 2, 3, 9, 1, 2, 8, 7]
start = time.time()
# 1. ์ค๋ณต ์ ๊ฑฐ
unique_numbers = []
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
time.sleep(0.1)
# 2. ์ ๋ ฌ (๋ฒ๋ธ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ)
n = len(unique_numbers)
for i in range(n):
for j in range(0, n-i-1):
if unique_numbers[j] > unique_numbers[j+1]:
# swap
unique_numbers[j], unique_numbers[j+1] = unique_numbers[j+1], unique_numbers[j]
end = time.time()
print(f"์ง์ ๊ตฌํ : {end-start:.4f}sec")
start = time.time()
# ์ค๋ณต ์ ๊ฑฐ
unique_numbers = list(set(numbers))
time.sleep(0.1)
# ์ ๋ ฌ
sorted_numbers = sorted(unique_numbers)
end = time.time()
print(f"๋ด์ฅํจ์ ์ฌ์ฉ : {end-start:.4f}sec")
์์ : ๋ฆฌ์คํธ ๋ด ์ต๋๊ฐ ์ฐพ๊ธฐ
import time
import numpy as np
if __name__ == "__main__":
numbers = [np.random.randint(0,1000) for _ in range(10000000)]
start = time.time()
# ์ด๊ธฐ ์ต๋๊ฐ์ ์ฒซ ๋ฒ์งธ ์์๋ก ์ค์
max_value = numbers[0]
# ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉฐ ์ต๋๊ฐ ์ฐพ๊ธฐ
for num in numbers:
if num > max_value:
max_value = num
end = time.time()
print(f"์ง์ ๊ตฌํ : {end-start:.4f}sec")
start = time.time()
# NumPy ๋ฐฐ์ด๋ก ๋ณํ ํ ์ต๋๊ฐ ์ฐพ๊ธฐ
max_value = np.max(numbers)
end = time.time()
print(f"Numpy : {end-start:.4f}sec")
์ด๋ ๊ฒ ๋ฐฐ์ด ์ฐ์ฐ์ ํ ๋ Numpy ๋ด์ฅ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ๋น ๋ฅธ ์ฐ์ฐ์ ํ ์ ์๋ค.
728x90
๋ฐ์ํ
'Langauge > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ๋ณ๋ ฌ ์ฒ๋ฆฌ (0) | 2024.09.13 |
---|---|
[Python] ์ฐ์ฐ ์๋ ์ฌ๋ฆฌ๊ธฐ - ํ๋กํ์ผ๋ง(Profiling) (0) | 2024.09.13 |
[Python] ์ฐ์ฐ ์๋ ์ฌ๋ฆฌ๊ธฐ - ๊ฐ์ ๋ฐ ์๊ณ ๋ฆฌ์ฆ๊ณผ ์๋ฃ๊ตฌ์กฐ (0) | 2024.09.12 |
[Python] 8. ํ์ผ ์ฒ๋ฆฌ (0) | 2023.02.11 |
[Python] 7. ์์ธ์ฒ๋ฆฌ (0) | 2023.02.11 |