forked from npqh243/parallel_huffman_encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman_block_canon.cpp
More file actions
70 lines (52 loc) · 2.17 KB
/
Copy pathhuffman_block_canon.cpp
File metadata and controls
70 lines (52 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <omp.h>
#include <iomanip>
#include "huffman.cpp"
using namespace std;
int main() {
cout << "--- MA HOA HUFFMAN (BLOCK + CANON) ---\n";
omp_set_num_threads(THREADS_NUM);
FILE* inpFile = fopen(FILE_NAME, "r");
if (!inpFile) {
cerr << "Khong the mo file input\n";
return 1;
}
fseek(inpFile, 0, SEEK_END);
long long fileSize = ftell(inpFile);
fseek(inpFile, 0, SEEK_SET);
double time0 = omp_get_wtime();
// --- BƯỚC 1: ĐẾM TẦN SUẤT ---
long long freq[256] = { 0 };
countFrequencyBlock(inpFile, fileSize, freq);
double time1 = omp_get_wtime();
cout << "Thoi gian chay: " << time1 - time0 << " giay\n";
// --- BƯỚC 2: DỰNG CÂY HUFFMAN ---
map<char, string> huffmanCode;
generateCanonCode(freq, huffmanCode);
double time2 = omp_get_wtime();
cout << "Thoi gian chay: " << time2 - time1 << " giay\n";
// --- BƯỚC 3: TÍNH KÍCH THƯỚC SAU NÉN ---
fseek(inpFile, 0, SEEK_SET);
long long totalBits = 0;
encodeBlock(inpFile, fileSize, totalBits, huffmanCode);
double time3 = omp_get_wtime();
cout << "Thoi gian chay: " << time3 - time2 << " giay\n";
fclose(inpFile);
// --- KẾT QUẢ ---
cout << "=== KET QUA BLOCK + CANON HUFFMAN ===\n";
cout << "Tong thoi gian chay: " << time3 - time0 << " giay\n";
double originalSizeMB = (double)fileSize / 1024 / 1024; // / KB / MB
double compressedSizeMB = (double)((totalBits / 8) + 1) / 1024 / 1024; // / KB / MB
double blockSizeMB = (double)BLOCK_SIZE / 1024 / 1024; // / KB / MB
cout << fixed << setprecision(2);
cout << "So luong song song: " << THREADS_NUM << "\n";
cout << "Kich thuoc block: " << blockSizeMB << " MB\n";
cout << "Kich thuoc TRUOC nen: " << originalSizeMB << " MB\n";
cout << "Kich thuoc SAU nen: " << compressedSizeMB << " MB\n";
cout << "Ti le nen: " << (compressedSizeMB / originalSizeMB) * 100 << "%\n";
cout << "Toc do xu ly: " << originalSizeMB / (time3 - time0) << " MB/s\n";
}