简体   繁体   English

为什么我的 List 在它不应该运行的时候比 Queue 运行得快?

[英]Why my List is running faster than Queue when it should not?

I am comparing enqueue and dequeue functions of following data structures in Python:我正在比较 Python 中以下数据结构的入出队函数:

1) List 1) 列表

using built in python list使用内置的python列表

I implemented enqueue using .insert(0,value)我使用.insert(0,value)实现入

I implemented dequeue using .pop()我使用.pop()实现了出队

2) Queue 2) 队列

using custom class I made for Queue using from collections import deque使用我使用from collections import deque为 Queue 创建的自定义类

I implemented enqueue using .enqueue(value)我使用.enqueue(value)实现了入

I implemented dequeue using .dequeue()我使用.dequeue()实现了出队

My code:我的代码:

import timeit

# Working For List :

List_Code = '''

def M_N_List(M, N):

    from random import randint

    no_of_integers = N

    list = []

    for i in range(M):

        for i in range(no_of_integers):
            list.insert(0 , randint(1, 100))

        for i in range(no_of_integers):
            list.pop()            
            
M_N_List(15,20)
            
'''

print("List : ")
print ("Code Run Time = " , round(timeit.timeit(stmt = List_Code,
                     number = 10000) , 4) , "seconds")

# *****************************************************************

# Working For Queue :

Queue_Code = '''

def M_N_Queue(M,N):

    from Queue import Queue
    from random import randint

    no_of_integers = N

    queue = Queue()

    for i in range(M):

        for i in range(no_of_integers):
            queue.enqueue(randint(1,100))

        for i in range(no_of_integers):
            queue.dequeue()
 
M_N_Queue(15,20)
 
'''

print("")
print("Queue : ")
print ("Code Run Time = " , round(timeit.timeit(stmt = Queue_Code,
                     number = 10000) , 4) , "seconds") 

My custom queue class:我的自定义队列类:

import ctypes
from collections import deque

class Queue:

    def __init__(self):
        self.buffer = deque()

    def size(self):
        return len(self.buffer)

    def make_array(self,new_cap):
        return (new_cap * ctypes.py_object)()

    def __getitem__(self, k):
        if not 0 <= k < len(self.buffer):
            return IndexError("k is out of bounds")

        return self.buffer[k]

    def enqueue(self, val):
        self.buffer.appendleft(val)

    def dequeue(self):
        return self.buffer.pop()

    def is_empty(self):
        return len(self.buffer) == 0

Problem:问题:

Despite knowing the fact that List is slower than Deque in python , I am getting the opposite results when I do my time analysis using timeit library.尽管知道List比 python 中的Deque慢,但当我使用timeit库进行时间分析时,我得到了相反的结果。

Queue is taking greater time(in seconds) than List in my analysis when it should not because as we know big-o of List is greater than that of Queue在我的分析中, QueueList花费更多的时间(以秒为单位),因为我们知道List 的big-o大于Queue

Kindly see my hand written analysis List Vs Queue Analysis.pdf for better understanding of my problem.请参阅我的手写分析List Vs Queue Analysis.pdf以更好地理解我的问题。

The slow down is caused by your wrapping Queue class, every time a method is called multiple attr lookups and further method calls are triggered and they all add up.速度变慢是由您的包装Queue类引起的,每次调用一个方法时都会进行多次 attr 查找并触发进一步的方法调用,并且它们都会加起来。 If you use a deque directly you will get faster results than with a list如果您直接使用deque ,您将获得比使用列表更快的结果

DEQUE_CODE = '''

def M_N_Deque(M,N):

    from collections import deque
    from random import randint

    no_of_integers = N

    queue = deque()

    for i in range(M):

        for i in range(no_of_integers):
            queue.appendleft(randint(1,100))

        for i in range(no_of_integers):
            queue.pop()

M_N_Deque(15,20)

'''

print("")
print("Deque : ")
print("Code Run Time = ", round(timeit.timeit(stmt=DEQUE_CODE,
                                              number=10000), 4), "seconds")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM