This shows you the differences between two versions of the page.
ep:labs:03:contents:tasks:ex3 [2025/02/11 23:28] cezar.craciunoiu created |
ep:labs:03:contents:tasks:ex3 [2025/03/17 20:53] (current) radu.mantu [03. [15p] Zip with compression levels] |
||
---|---|---|---|
Line 10: | Line 10: | ||
=== [10p] Task A - Measurements === | === [10p] Task A - Measurements === | ||
- | Write a script to measure the compression rate and the time required for each level. Use the following files: | + | Write a script to measure the compression rate and the time required for each level. You have a few large files in the code skeleton but feel free to add more. If you do add new files, make sure that they are not random data! |
- | * two largest bitmaps from [[https://www.fileformat.info/format/bmp/sample/index.htm|here]] | + | |
- | * this large text file [[https://norvig.com/big.txt|here]] | + | |
=== [5p] Task B - Plot === | === [5p] Task B - Plot === | ||
- | Fill the data you obtained into the **python3** script in {{:ep:labs:plot.zip|plot.zip}}. \\ | + | Generate a plot illustrating the compression rate, size decrease, etc. as a function of **zip** compression level. Make sure that your plot is //understandable// (i.e., has labels, a legend, etc.) Make sure to average multiple measurements for each compression level. |
- | Make sure you have **python3** and **python3-matplotlib** installed. | + | |
- | + | ||
- | <solution -hidden> | + | |
- | **Task A:** | + | |
- | + | ||
- | <code bash> | + | |
- | #!/bin/bash | + | |
- | + | ||
- | # clean up base files directory and archives | + | |
- | rm -rf file* | + | |
- | mkdir files | + | |
- | + | ||
- | # download some large files (2 images, 1 text) | + | |
- | ( cd files \ | + | |
- | ; printf "\033[33mStarting file download...\n\tPart 1/3 ... " \ | + | |
- | ; wget -q -O image_1.bmp https://www.fileformat.info/format/bmp/sample/4cb74cda027a43f3b278c05c3770950f/download \ | + | |
- | ; printf "\033[32mdone\n\t\033[33mPart 2/3 ... " \ | + | |
- | ; wget -q -O image_2.bmp https://www.fileformat.info/format/bmp/sample/1d71eff930af4773a836a32229fde106/download \ | + | |
- | ; printf "\033[32mdone\n\t\033[33mPart 3/3 ... " \ | + | |
- | ; wget -q -O text_file.txt https://norvig.com/big.txt \ | + | |
- | ; printf "\033[32mdone\n\033[0m" | + | |
- | ) | + | |
- | + | ||
- | # archive folder | + | |
- | for i in $(seq 0 9); do | + | |
- | if [ ${i} -eq 6 ]; then | + | |
- | printf "\n\033[31m >>> Default compression level <<<" | + | |
- | fi | + | |
- | + | ||
- | printf "\n\033[33mStarting: \033[1mzip -r -${i} files-${i}.zip files/\n\033[0m" | + | |
- | + | ||
- | time zip -r -${i} files-$i.zip files | + | |
- | done | + | |
- | </code> | + | |
- | + | ||
- | **Task B:** | + | |
- | <code python> | + | |
- | #!/usr/bin/python3 | + | |
- | + | ||
- | import matplotlib.pyplot as plt | + | |
- | + | ||
- | + | ||
- | def main(): | + | |
- | # TODO: for each compression level in {0, 1, ..., 9}: | + | |
- | # fill real_time [ms] | + | |
- | # fill archive_size [bytes] | + | |
- | level = list(range(10)) | + | |
- | real_time = [ 47, 251, 278, 356, 350, 489, 688, 810, 1027, 1074 ] | + | |
- | archive_size = [ 12193688, 5762873, 5570296, 5401227, 5307842, | + | |
- | 5185863, 5130121, 5118616, 5111763, 5111482 ] | + | |
- | + | ||
- | archive_size = [it / 1024 for it in archive_size] | + | |
- | + | ||
- | # plot data | + | |
- | fix, axs = plt.subplots(2) | + | |
- | + | ||
- | axs[0].plot(level, real_time, 'bo-') | + | |
- | axs[1].plot(level, archive_size, 'ro-') | + | |
- | + | ||
- | axs[0].set_xticks(level) | + | |
- | axs[1].set_xticks(level) | + | |
- | + | ||
- | axs[0].grid(True, which='both') | + | |
- | axs[1].grid(True, which='both') | + | |
- | + | ||
- | axs[0].set_xlabel('Compression level') | + | |
- | axs[1].set_xlabel('Compression level') | + | |
- | + | ||
- | axs[0].set_ylabel('Time [ms]') | + | |
- | axs[1].set_ylabel('Archive size [byte]') | + | |
- | + | ||
- | plt.show() | + | |
- | + | ||
- | + | ||
- | if __name__ == '__main__': | + | |
- | main() | + | |
- | </code> | + | |
- | </solution> | + |