Python递归二进制搜索功能

时间:2015-09-07 05:49:31

标签: python algorithm search binary analysis

问:基本上......创建一个二元搜索算法,它与3种不同语言相同(除了编程语言),对于8个不同大小的数组(128,512,...,52428)进行相同的8,000,000次不成功搜索,(524288 * 4)= 2097152)。 我编写这个问题的前两种语言是C和C ++并得到了正常的结果:2097152大小的数组不超过3s

我决定尝试使用Python,因为我从来没有对它进行过编码,并希望尝试一下。我最终得到荒谬的时间来完成算法:

-For 128 elements I got 800s
-512 elements = 1005s
-2048 elements = 1682s
-8192 elements = 4152s (my computer went to sleep so this may be the sudden increase)
-32768 elements = 1714s
-131072 elements = 1890s
-524288 = 2074s
-2097152 = still running!!

(除了具有8192个元素的数组之外,这基本上遵循O(log(base2)n):平均每次递归检查需要114s。

这是我第一次使用Python进行编码,所以我想知道:我的代码效率低(尽管算法非常基本),或者Python无法处理递归调用以及C / C ++ / Java,尤其是他们变得非常大?任何帮助/见解都会很棒。我的Python代码如下:

import time
def binarySearch(array, key, min, max):
    mid = int(min + ((max - min) / 2))
    if min >= max:
        return False
    elif array[mid] == key:
        return True
    elif array[mid] > key:
        return binarySearch(array, key, min, mid - 1)
    elif array[mid] < key:
        return binarySearch(array, key, mid + 1, max)

i = 128
#for i in range(128, 2097152):
while i <= 2097152:
    sTime = time.time()
    myArray = [None] * (i-1)
    for k in range(0, i-1):
        myArray[k] = 13
    eTime = time.time()
    k = 0
    startTime = time.time()
    for k in range(0, 8000000):
        binarySearch(myArray, 100, 0, i-1)
    endTime = time.time()
    print("[Size = ", i, "] 8,000,000 Unsucessful Searches took ", endTime - startTime, " seconds\n")
    i = i*4

2 个答案:

答案 0 :(得分:0)

使用Python解决性能问题有两种方法:

  • 使用CPython C APICython
  • 使用尽可能多的内置功能优化代码,然后将代码提供给PyPy

如果我们采取第二种方式(请注意//运算符而不是intlist generator以加快列表速度和PEP8格式化:

import time


def binary_search(array, key, min_, max_):
    mid = min_ + ((max_ - min_) // 2)
    if min_ >= max_:
        return False
    elif array[mid] == key:
        return True
    elif array[mid] > key:
        return binary_search(array, key, min_, mid - 1)
    elif array[mid] < key:
        return binary_search(array, key, mid + 1, max_)

i = 128
magic = 13
my_array = [magic for _ in range(i-1)]
while i <= 2097152:
    k = 0
    start_time = time.time()
    for k in range(0, 8000000):
        binary_search(my_array, 100, 0, i-1)
    end_time = time.time()
    print("[Size = ", i, "] 8,000,000 Unsucessful Searches took ", end_time - start_time, " seconds\n")

    s_time = time.time()
    my_array += [magic for _ in range(3 * i)]
    i = i*4
    e_time = time.time()

毕竟,这是我从PyPy得到的:

D:\>pypy test.py
[Size =  128 ] 8,000,000 Unsucessful Searches took  0.1699998378753662  seconds
[Size =  512 ] 8,000,000 Unsucessful Searches took  0.9229998588562012  seconds
[Size =  2048 ] 8,000,000 Unsucessful Searches took  1.0799999237060547  seconds
[Size =  8192 ] 8,000,000 Unsucessful Searches took  1.2969999313354492  seconds
[Size =  32768 ] 8,000,000 Unsucessful Searches took  1.5120000839233398  seconds
[Size =  131072 ] 8,000,000 Unsucessful Searches took  1.7920000553131104  seconds
[Size =  524288 ] 8,000,000 Unsucessful Searches took  2.062000036239624  seconds
[Size =  2097152 ] 8,000,000 Unsucessful Searches took  2.236999988555908  seconds

答案 1 :(得分:0)

请记住,C / C ++是编译语言,Python是解释型语言。 Python不仅执行指令,还会读取它们并将它们动态转换为可执行代码。减速的另一个原因是循环结构本身。性能比为1000并不奇怪。