의 성능 :파이썬 : 찾는 제수 이러한 기능을 벤치마킹
def divisors_optimized(number):
square_root = int(math.sqrt(number))
for divisor in range(1, square_root):
if number % divisor == 0:
yield divisor
yield number/divisor
if square_root ** 2 == number:
yield square_root
def number_of_divisors_optimized(number):
count = 0
square_root = int(math.sqrt(number))
for divisor in range(1, square_root):
if number % divisor == 0:
count += 2
if square_root ** 2 == number:
count += 1
return count
당신은 기본 구조가 모두 동일하다고 볼 수 있습니다.
벤치 마크 코드 :
number = 9999999
for i in range(10):
print(f"iteration {i}:")
start = time.time()
result = list(utils.divisors_optimized(number))
end = time.time()
print(f'len(divisors_optimized) took {end - start} seconds and found {len(result)} divisors.')
start = time.time()
result = utils.number_of_divisors_optimized(number)
end = time.time()
print(f'number_of_divisors_optimized took {end - start} seconds and found {result} divisors.')
print()
출력 :
iteration 0:
len(divisors_optimized) took 0.00019598007202148438 seconds and found 12 divisors.
number_of_divisors_optimized took 0.0001919269561767578 seconds and found 12 divisors.
iteration 1:
len(divisors_optimized) took 0.00019121170043945312 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00020599365234375 seconds and found 12 divisors.
iteration 2:
len(divisors_optimized) took 0.000179290771484375 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00019049644470214844 seconds and found 12 divisors.
iteration 3:
len(divisors_optimized) took 0.00019025802612304688 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00020170211791992188 seconds and found 12 divisors.
iteration 4:
len(divisors_optimized) took 0.0001785755157470703 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00017905235290527344 seconds and found 12 divisors.
iteration 5:
len(divisors_optimized) took 0.00022721290588378906 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00020170211791992188 seconds and found 12 divisors.
iteration 6:
len(divisors_optimized) took 0.0001919269561767578 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00018930435180664062 seconds and found 12 divisors.
iteration 7:
len(divisors_optimized) took 0.00017881393432617188 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00017905235290527344 seconds and found 12 divisors.
iteration 8:
len(divisors_optimized) took 0.00017976760864257812 seconds and found 12 divisors.
number_of_divisors_optimized took 0.0001785755157470703 seconds and found 12 divisors.
iteration 9:
len(divisors_optimized) took 0.00024819374084472656 seconds and found 12 divisors.
number_of_divisors_optimized took 0.00020766258239746094 seconds and found 12 divisors.
당신은 실행 시간이 매우 가까운 것을 볼 수 있습니다 때마다 어느 선호.
누군가 제너레이터에서 목록을 만들고 그 길이를 검색하는 방법이 반복하는 동안 계산하는 것과 똑같은 방법을 설명 할 수 있습니까? 내 말은, 메모리 할당 (list()
)이 할당보다 훨씬 비쌉니까?
저는 파이썬 3.6.3을 사용하고 있습니다.
목록을 만드는 것이 더 느리지는 않습니다. 사실 더 빨라질 수도 있습니다. 그러나 목록은 메모리를 사용합니다. 이는 사람들이 가능하면 피하는 주된 이유입니다. –
정적으로 형식화되고 대부분 컴파일 된 언어 배경을 사용하면 메모리 할당이 비싸다고 생각할 수 있습니다. JITless, 동적 CPython의 다른 모든 오버 헤드와 관련하여 메모리 할당은 양동이의 드롭입니다. – user2357112