示例#1
0
    def __init__(self, operations, search, insert, delete, bm_start, bm_length,
                 bm_interval):
        """Performs the operations sequence listed, producing the plot points listed."""

        super(MixedSIDBenchmarkPlot, self).__init__()

        self.operations = operations
        self.search = search
        self.insert = insert
        self.delete = delete
        self.bm_start = bm_start
        self.bm_length = bm_length
        self.bm_interval = bm_interval

        # First, let's go up to bm_start.
        for i in range(bm_start):
            op = operations[i]
            self.doOperation(delete, insert, search, op)

        # Now, we'll do the standard benchmark loop....
        # For the remainder of the benchmark cycle, we will use next_benchmark to track when to start a benchmark
        # and p and q to represent the start and end, respectively, of a range that we're calculating.
        p = bm_start
        while p < len(operations):
            # Don't overshoot stop when benchmarking.
            q = min(p + bm_length, len(operations))
            time = Plot.benchmark(
                lambda x: self.doOperation(delete, insert, search, x),
                operations, p, q)
            self.coordinates.append((p, time))
            next_benchmark = p + bm_interval

            # Then, advance to the next benchmark
            p = q
            q = min(next_benchmark, len(operations))
            for i in range(p, q):
                self.doOperation(delete, insert, search, operations[i])

            # Before looping, update p.
            p = q
示例#2
0
    def _doBenchmark(
        self,
        search,
        insert,
        delete,
        search_samples,
        insert_samples,
        delete_samples,
        stop,
        bm_start,
        bm_length,
        bm_interval,
    ):
        """Helper function. Performs the benchmark routine described elsewhere."""

        # Initialize indices.
        search_index = insert_index = delete_index = 0

        # 0. Set next_benchmark as bm_start.
        next_benchmark = bm_start

        # 1. Call insert on the items in insert_samples with indices [0, bm_start).
        print("Inserting up to {}".format(bm_start))
        for i in range(0, bm_start):
            insert(insert_samples[insert_index])
            insert_index += 1

        # 2. While insert_index < stop:
        while insert_index < stop:
            # a. set last_benchmark as next_benchmark.
            last_benchmark = next_benchmark

            # b. Set next_benchmark as min(next_benchmark + bm_interval, stop).
            next_benchmark = min(next_benchmark + bm_interval, stop)

            # c. Benchmark: time bm_length searches on items in search_samples[search_index]. Divide the total time by
            #    bm_length and save (data structure size, time) as a plot point for insert.
            print("Benchmarking: search for items {} to {}".format(
                search_index, search_index + bm_length))
            self.search_plot.plotPoint(
                insert_index,
                Plot.benchmark(search, search_samples, search_index,
                               search_index + bm_length))
            search_index += bm_length

            # d. Benchmark: time bm_length inserts on items in insert_samples[insert_index]. Divide the total time by
            #    bm_length and save (data structure size before timed insertions, time) as a plot point for insert.
            print("Benchmarking: insert items {} to {}".format(
                insert_index, insert_index + bm_length))
            self.insert_plot.plotPoint(
                insert_index,
                Plot.benchmark(insert, insert_samples, insert_index,
                               insert_index + bm_length))
            insert_index += bm_length

            # e. Insert subsequent items from insert_samples, incrementing insert_index, until
            #    insert_index == next_benchmark. These insertions are not timed.
            print("Inserting intermediate values up to {}".format(
                next_benchmark))
            while insert_index < next_benchmark:
                insert(insert_samples[insert_index])
                insert_index += 1

        # 3. Set next_benchmark = last_benchmark + bm_length.
        next_benchmark = last_benchmark + bm_length

        # 4. While data structure size > bm_start:
        # We could reuse insert_index, but this is VASTLY clearer.
        data_structure_size = insert_index
        while data_structure_size > bm_start:
            # a. Delete items from delete_samples[delete_index], incrementing delete_index, until
            #    data structure size == next_benchmark.
            print("Deleting vlues down to {}".format(next_benchmark))
            while data_structure_size > next_benchmark:
                delete(delete_samples[delete_index])
                delete_index += 1
                data_structure_size -= 1

            # b. Benchmark: time bm_length deletions on items in delete_samples[delete_index], incrementing
            #    delete_index each time. Divide the total time by bm_length and save
            #    (data structure size after timed deletions, time) as a plot point for delete.
            print("Benchmarking: delete items {} to {}".format(
                delete_index, delete_index + bm_length))
            data_structure_size -= bm_length
            self.delete_plot.plotPoint(
                data_structure_size,
                Plot.benchmark(delete, delete_samples, delete_index,
                               delete_index + bm_length))
            delete_index += bm_length

            # c. Decrease next_benchmark by bm_interval.
            next_benchmark -= bm_interval