Here is my code for the below packet time simulation problem using queue. My code works correctly but it's slow, please help with suggestions to improve run time and other suggestions for performance improvement.
Problem: You are given a series of incoming packets in an array of [t_a, t_d] pairs (one pair for each packet) where t_a is arrival time of the packet and t_d is the processing time required for that packet. The processor has a buffer with a fixed size and when full, packets are dropped. For each packet, output either the start of process time or -1 if the packet is dropped. Processor only processes one packet at the time and packet arrival times are non-decreasing.
eg. input of [ [0, 2], [1, 4], [5, 3] ] with buffer size of 2 gives output equal to [0, 2, 6].
def process_packets(packets, buffSize): # base case for packet size or buffSize == 0 if packets ==  or buffSize == 0: return  # initializing parameters: # ts_te is array of [ts,te] pairs where ts = start of packet process time # and te = end of packet process time # length of ts_te array is treated as buffer size ts_te = [ [ packets, packets + packets ] ] # results is either start of packet process time (if packet makes it to buffer) # or -1 for packets when buffer is full at their arrival time results = [ ts_te ] # since first packet is processed in initialization, I remove it packets = packets[1:] for packet in packets: packetArr = packet packetDur = packet # processor's start time is either end of last packet in buffer or # new packet's arrival time, whichever is bigger startTime = max ( ts_te[-1], packetArr ) # removing all packets that have been processed from buffer when new # packet arrives for elem in ts_te: if packetArr >= elem: ts_te.pop(0) # if there is room in the buffer, add new packets [ts,te] information # and append process time start to result if len(ts_te) < buffSize: ts_te.append( [startTime , startTime+packetDur] ) results.append(startTime) else: results.append(-1) return results