Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar10 / src / hashmap / tests.cpp
diff options
context:
space:
mode:
Commit3c72a02284dad4ddd9df1ff6910ed1a0a4550a30 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2024-12-22 13:19:05 -0500
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2024-12-22 13:19:05 -0500
Filese5921b4ff115ae1a1cd570559ea539ecae5a0962 /Seminar10/src/hashmap/tests.cpp
Data-structures-and-algorithms-3c72a02284dad4ddd9df1ff6910ed1a0a4550a30.tar
Data-structures-and-algorithms-3c72a02284dad4ddd9df1ff6910ed1a0a4550a30.tar.gz
Data-structures-and-algorithms-3c72a02284dad4ddd9df1ff6910ed1a0a4550a30.zip
Parentd8fb59b4d45197c9b3a2b100a3d94b1c2e92b2ef (diff)
Add seminar 10 and 11
1 files changed, 168 insertions, 0 deletions
-rw-r--r--Seminar10/src/hashmap/tests.cpp168
diff --git a/Seminar10/src/hashmap/tests.cpp b/Seminar10/src/hashmap/tests.cpp
new file mode 100644
index 0000000..9da1cde
--- /dev/null
+++ b/Seminar10/src/hashmap/tests.cpp
@@ -0,0 +1,168 @@
+#include "linear-probing.hpp"
+#include "separate-chaining.hpp"
+#include <chrono>
+#include <fstream>
+#include <iostream>
+#include <unordered_map>
+#include <map>
+
+std::vector<std::string> get_words(const std::string& file)
+{
+ std::vector<std::string> words;
+ std::ifstream ifs("harry.txt");
+
+ if (!ifs.is_open())
+ {
+ std::cout << "File retrieval failed" << std::endl;
+ return {};
+ }
+
+ while (!ifs.eof())
+ {
+ std::string word;
+ ifs >> word;
+ words.emplace_back(word);
+ }
+
+ return words;
+}
+
+void test_hashmap_separate_chaining(size_t initial_capacity, double lf)
+{
+ std::cout << "[Separate chaining tests]: Running test for " << initial_capacity << " initial capacity and " << lf
+ << " load factor." << std::endl;
+
+ separate_chaining_hashmap<std::string, int> mp;
+ std::vector<std::string> words = get_words("harry.txt");
+
+ std::cout << "[Separate chaining tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
+
+ unsigned dict_size = 0;
+ auto begin = std::chrono::high_resolution_clock::now();
+ for (const auto& word : words)
+ {
+ try
+ {
+ ++mp.modify(word);
+ }
+ catch (...)
+ {
+ ++dict_size;
+ mp.insert(word, 1);
+ }
+ }
+
+ auto end = std::chrono::high_resolution_clock::now();
+ auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
+
+ std::cout << "[Separate chaining tests]: Parsed " << words.size() << " words in separate_chaining_hashmap<std::string, int>."
+ << std::endl;
+ std::cout << "[Separate chaining tests]: Dict size: " << dict_size << std::endl;
+ std::cout << "[Separate chaining tests]: Buckets count: " << mp.bucket_count() << std::endl;
+ std::cout << "[Separate chaining tests]: Size: " << mp.size() << std::endl;
+ std::cout << "[Separate chaining tests]: Time: " << duration << " ms." << std::endl;
+}
+
+void test_hashmap_linear_probing(size_t jump_value, size_t initial_capacity, double lf)
+{
+ std::cout << "[Linear probing tests]: Running test for " << initial_capacity << " initial capacity and " << lf
+ << " load factor and " << jump_value << " as jump value." << std::endl;
+
+ linear_probing_hashmap<std::string, int> mp(initial_capacity, lf, jump_value);
+ std::vector<std::string> words = get_words("harry.txt");
+
+ std::cout << "[Linear probing tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
+
+ int dict_size = 0;
+ auto begin = std::chrono::high_resolution_clock::now();
+ for (const auto& word : words)
+ {
+ try
+ {
+ ++mp.modify(word);
+ }
+ catch (...)
+ {
+ ++dict_size;
+ mp.insert(word, 1);
+ }
+ }
+
+ auto end = std::chrono::high_resolution_clock::now();
+
+ auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
+
+ std::cout << "[Linear probing tests]: Parsed " << words.size() << " words in linear_probing_hashmap<std::string, int>."
+ << std::endl;
+ std::cout << "[Linear probing tests]: Dict size: " << dict_size << std::endl;
+ std::cout << "[Linear probing tests]: Buckets count: " << mp.bucket_count() << std::endl;
+ std::cout << "[Linear probing tests]: Size: " << mp.size() << std::endl;
+ std::cout << "[Linear probing tests]: Time: " << duration << " ms." << std::endl;
+}
+
+
+void test_hashmap_std()
+{
+ std::unordered_map<std::string, int> mp;
+ std::vector<std::string> words = get_words("harry.txt");
+
+ std::cout << "[std::unordered_map tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
+
+ auto begin = std::chrono::high_resolution_clock::now();
+ for (const auto& word : words)
+ {
+ ++mp[word];
+ }
+
+ auto end = std::chrono::high_resolution_clock::now();
+
+ auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
+
+ std::cout << "[std::unordered_map tests]: Parsed " << words.size() << " words in std::unordered_map<std::string, int>."
+ << std::endl;
+
+ std::cout << "[std::unordered_map tests]: Buckets count: " << mp.bucket_count() << std::endl;
+ std::cout << "[std::unordered_map tests]: Size: " << mp.size() << std::endl;
+ std::cout << "[std::unordered_map tests]: Time: " << duration << " ms." << std::endl;
+}
+
+void test_map_std()
+{
+ std::map<std::string, int> mp;
+ std::vector<std::string> words = get_words("harry.txt");
+
+ std::cout << "[std::map tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
+
+ auto begin = std::chrono::high_resolution_clock::now();
+ for (const auto& word : words)
+ {
+ ++mp[word];
+ }
+
+ auto end = std::chrono::high_resolution_clock::now();
+
+ auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
+
+ std::cout << "[std::map tests]: Parsed " << words.size() << " words in std::map<std::string, int>."
+ << std::endl;
+
+ std::cout << "[std::map tests]: Size: " << mp.size() << std::endl;
+ std::cout << "[std::map tests]: Time: " << duration << " ms." << std::endl;
+}
+
+int main()
+{
+ test_hashmap_separate_chaining(1000, 0.7);
+
+ std::cout << std::endl;
+
+ test_hashmap_linear_probing(1, 997, 0.7);
+
+ std::cout << std::endl;
+
+ test_hashmap_std();
+
+ std::cout << std::endl;
+
+ test_map_std();
+} \ No newline at end of file