Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import random import time from queues import BoundedQueue from queues import CircularQueue from decimal import Decimal from terminalplot import plot2 def enqueue_experiment(queue_class , queue_size ):

image text in transcribed

import random import time from queues import BoundedQueue from queues import CircularQueue from decimal import Decimal from terminalplot import plot2

def enqueue_experiment(queue_class , queue_size ): ''' This function will enqueue elements into Bounded and Circular queues and return them :param queue_class: The queue_class description that we want to declare the object for like BoundedQueue, CircularQueue :param queue_size: The size of the queue and the number of elements that we want to enter in queue :return: Bounded Queue Object, Circular Queue Object ''' # init the relevant objects queue = queue_class(queue_size)

# enqueue elements to max capacity of queue while not queue.is_full(): value = random.random() queue.enqueue(value)

return queue

def dequeue_experiment(queue): ''' This function will be responsible for dequeues the elements in the queue :param queue: (queue object) The queue object that we want to test on :return: (float) Returns the time of execution of the dequeue operation ''' time = 0.0 # the time to dequeue all the elements ##### START CODE HERE #####

##### END CODE HERE ###### return time

def avg_dequeue_experiment(queue_class, size, sample_size = 5): ''' This function is used to average over many runs for the dequeue experiment :param queue_class: (function-type) The typeof queue that we are using :param size: (int) Size of the queue to do experiment :param sample_size : (int) THe number of samples to be used for averaging :return: (float) return the average time ''' q_run_avg = 0 for i in range(sample_size): queue = enqueue_experiment(queue_class, size) q_run_avg += dequeue_experiment(queue)

return float(q_run_avg)/sample_size

def main(): # queue sizes that we will use for different experiments # queues_sizes = [10000, 20000 , 30000, 40000, 50000, 60000, 70000,80000, 90000, 100000] queues_sizes = [10000, 30000, 50000, 70000, 90000, 100000]

# the setup of the experiment bqueues = [] cqueues = [] for q in queues_sizes: # print("Done for Queue Size : {}".format(q)) bq = enqueue_experiment(BoundedQueue, q) cq = enqueue_experiment(CircularQueue,q) bqueues.append(bq) cqueues.append(cq)

# perform the dequeue experiment bounded_queue_times = [dequeue_experiment(q) for q in bqueues] circular_queue_times = [dequeue_experiment(q) for q in cqueues]

# print table of results for one run each (not averaged) print("Times for Bounded and Circular Queue without using Average") line = '-'*(13 + 14*len(queues_sizes)) line2 = str('-'*13 + '+')*(1 + len(queues_sizes)) print(line) print(str(" Queue Size | " + ' '.join(" {:

# plot the terminal graph try: print(''' Legend : '*' : Points of the Bounded Queue '#' : Points of Circular Queue '+' : Points where both coincide''')

plot2(queues_sizes, bounded_queue_times , circular_queue_times) except: print("Not able to print graph. Continuing .....")

# run experiment using averages avg_b_queue_times = [avg_dequeue_experiment(BoundedQueue, size) for size in queues_sizes] avg_c_queue_times = [avg_dequeue_experiment(CircularQueue, size) for size in queues_sizes]

# display table of averaged results (multiple runs for each data point) print("Times for Bounded and Circular Queue with Averaging") print(line) print(str(" Queue Size | " + ' '.join(" {:

try: # plot the graph print(''' Legend : '*' : Points of the Bounded Queue '#' : Points of Circular Queue '+' : Points where both coincide''')

plot2(queues_sizes, avg_b_queue_times, avg_c_queue_times) except: print("Not able to print graph. Continuing .....")

if __name__ == '__main__': # calculate total program run time start = time.time() main() end = time.time() print("The program took {} seconds to run".format(end - start))

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

1. Download and save labb_efficiency.py and terminalplot.py. (Save in the same folder as your queue.py from Exercise 1.) In this file, both your Bounded and Circular Queues have been imported from queues.py. This file also contains 4 function definitions: 3 have already been completed for you (enqueue_experiment, average_dequeue_experiment, and main). Read through the comments of those 3 functions to understand what they are doing 2. Complete the function dequeue_experiment (queue) so that it removes the first item in the queue, and continues to do so until that queue is empty. Note that the queue is passed in as an input to the function - so you do NOT have to create it or fill it with data in this function. Use a function from either the time or timeit modules to measure how long it takes to dequeue all of the items in the queue, and return that time measurement. Hint: there is an example of how to use time.time () at the bottom of lab6_efficiency.py. The time module returns times in seconds. import time start = time.time () # The statement(s) that you want to test end = time.time () time_interval = end - start 3. Run labb_efficiency. (This may take some time to run.) This will run a number of experiments, measuring the time it takes to dequeue all items from Bounded and Circular Queues of increasing capacities. These times will be printed in a table for you to view. The data should also be plotted for you, with time on the y-axis, and n on the x-axis (where n is the number of dequeues made). If a plot is not displayed (i.e. if you see a message that says Not able to print graph. Continuing..."), plot the data from the table using a spreadsheet program. You can run more experiments with larger queue capacities to get more values for your graph, but keep in mind that doing so will increase the runtime of the overall program. Which has a more efficient dequeue method: the Bounded Queue, or the Circular Queue? Base code from here : https://github.com/kressi/terminalplot/blob/master/terminalplot/terminalplot. py #import fcntl import os #import termios. import struct def plot_multiple(x1, y1, y2 , rows=None, columns=None): The first y1 will be '*' and the second will be '#' x, y list of values on x- and y-axis plot those values within canvas size (rows and columns) if not rows or not columns: rows, columns = get_terminal_size() # these might not be required arows = rows + 2 acols = columns + 4 # offset for caption rows -- 4 # Scale points such that they fit on canvas x_scaled = xscale(x, columns) y1_scaled, y2_scaled = yscale(y1, y2, rows) print (x_scaled) print(y1_scaled) print(y2_scaled) # Create empty canvas canvas = [[' for _ in range(columns)] for - in range(rows) ] # We can trry to print the axes # canvas (columns-1][0] = '0' # for i in range(columns-4,0): canvas [1] [i] = '_'. # for i in range(1, rows) : canvas[i] [4] = 'I' # # Add scaled points to canvas for first plot for ix, iy, in zip(x_scaled, y1_scaled): canvas (rows - iy - 1] [ix] = '*' # print for the second plot for ix, iy, in zip(x_scaled, y2_scaled): if canvas [rows - iy. - 1] [ix] == '*': canvas [rows - iy. - 1] [ix] = '#' else: canvas [rows - iy. - 1] [ix] = '+' # testing # canvas (0) (0] = '' # canvas [0] (columns - 1] = '1' # canvas [rows - 1][0] = '2' # canvas [rows - 1] [columns - 1] = '3'. # we can replace the common point with another type of label # Print rows of canvas for row in ('.join(row) for row in canvas]: print (row) # Print scale print(' join( [ Min x: ', str(min(x)), " Max x:', str(max(x)), # ' Miny: ', str(min(y)), # ' Max y: ', str(max(y)) Min y: 'str(min(min(y1), min(y2))), Max y: ', str(max(max(y1), max(y2))) ])) def plot_multiple2(x1, yi, y2, rows=None, columns=None): Trying to print the axis as well The first y1 will be '*' and the second will be '#' X, y list of values on X- and y-axis plot those values within canvas size (rows and columns) if not rows or not columns : rows, columns = get_terminal_size(). # offset for caption rows -= 4 # these might not be required arows = rows + 2 acols = columns + 4 # Scale points such that they fit on canvas x_scaled = xscale(x, columns - 4) y1_scaled, y2_scaled = yscale(y1, y2, rows) # print(x_scaled) # print(y1_scaled) # print(y2_scaled) # Create empty canvas canvas = [[ i for _ in range(columns)] for _ in range(arows)] # # We can trry to print the axes # canvas (columns-1][0] = '0'. # for i in range(columns-4,0): canvas [1] [i] = '' for i in range(1, rows): canvas [i] [4] = 'I' # # # # Add scaled points to canvas for first plot for ix, iy in zip(x_scaled, y1_scaled): canvas [rows - iy - 1] [ix + 4] = '*' # i = 0 for ix, iy in zip(x_scaled, y1_scaled): if ix

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions