|
| 1 | + |
| 2 | +from matplotlib import pyplot as plt |
| 3 | +import numpy as np |
| 4 | +from cobs import cobs |
| 5 | +from cobs import cobsr |
| 6 | + |
| 7 | + |
| 8 | +def cobsr_overhead_calc(num_bytes): |
| 9 | + return 257./256 - (255./256)**num_bytes |
| 10 | + |
| 11 | +def cobsr_overhead_measure(num_bytes): |
| 12 | + # TODO: review value |
| 13 | + NUM_TESTS = 10000 |
| 14 | + overhead = 0 |
| 15 | + for _i in xrange(NUM_TESTS): |
| 16 | + output = cobsr.encode(np.random.bytes(num_bytes)) |
| 17 | + overhead += (len(output) - num_bytes) |
| 18 | + return overhead / float(NUM_TESTS) |
| 19 | + |
| 20 | +def cobs_overhead_measure(num_bytes): |
| 21 | + # TODO: review value |
| 22 | + NUM_TESTS = 10000 |
| 23 | + overhead = 0 |
| 24 | + for _i in xrange(NUM_TESTS): |
| 25 | + output = cobs.encode(np.random.bytes(num_bytes)) |
| 26 | + overhead += (len(output) - num_bytes) |
| 27 | + return overhead / float(NUM_TESTS) |
| 28 | + |
| 29 | +fig = plt.figure() |
| 30 | +ax1 = fig.add_subplot(111) |
| 31 | + |
| 32 | +# x-range for plot |
| 33 | +num_bytes_list = np.arange(1, 30) |
| 34 | + |
| 35 | +# Calculate values and plot |
| 36 | + |
| 37 | +# Measured values for COBS |
| 38 | +#cobs_measured_overhead = [ cobs_overhead_measure(num_bytes) for num_bytes in num_bytes_list ] |
| 39 | +#ax1.plot(num_bytes_list, cobs_measured_overhead, 'g.') |
| 40 | + |
| 41 | +# Measured values for COBS/R |
| 42 | +cobsr_measured_overhead = [ cobsr_overhead_measure(num_bytes) for num_bytes in num_bytes_list ] |
| 43 | +ax1.plot(num_bytes_list, cobsr_measured_overhead, 'r.') |
| 44 | + |
| 45 | +# Calculated values for COBS/R |
| 46 | +cobsr_calc_overhead = [ cobsr_overhead_calc(num_bytes) for num_bytes in num_bytes_list ] |
| 47 | +ax1.plot(num_bytes_list, cobsr_calc_overhead, 'b.') |
| 48 | + |
| 49 | +ax1.set_xlabel('message length (bytes)') |
| 50 | +ax1.set_xlim(min(num_bytes_list), max(num_bytes_list)) |
| 51 | + |
| 52 | +# Make the y-axis label and tick labels match the line color. |
| 53 | +ax1.set_ylabel('encoding overhead (bytes)') |
| 54 | +if 0: |
| 55 | + ax1.set_ylabel('encoding overhead (bytes)', color='b') |
| 56 | + for tl in ax1.get_yticklabels(): |
| 57 | + tl.set_color('b') |
| 58 | + |
| 59 | +plt.show() |
0 commit comments