Fix inputs and add methods
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -0,0 +1,90 @@
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
int N = 1000000; // 1,000,000 tries
|
||||
int L = 20; // string length
|
||||
|
||||
string random_string(size_t length) {
|
||||
static const string chars =
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
|
||||
static random_device rd;
|
||||
static mt19937 gen(rd());
|
||||
|
||||
uniform_int_distribution<> dist(0, chars.size() - 1);
|
||||
|
||||
string s;
|
||||
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
s += chars[dist(gen)];
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void test_array(int length) {
|
||||
std::unordered_map<std::string, int> hash_map;
|
||||
hash_map.reserve(length * 2); // Reserve more to avoid rehashing
|
||||
std::vector<std::pair<std::string, int>> array;
|
||||
array.reserve(length);
|
||||
|
||||
vector<string> keys;
|
||||
|
||||
for (int i = 0; i < length; ++i) {
|
||||
string key = random_string(L);
|
||||
|
||||
keys.push_back(key);
|
||||
|
||||
hash_map[key] = i;
|
||||
array.emplace_back(key, i);
|
||||
}
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
for (int i = 0; i < N; ++i) {
|
||||
string &key = keys[i % length];
|
||||
|
||||
auto it = hash_map.find(key);
|
||||
|
||||
volatile int value = it->second;
|
||||
(void)value;
|
||||
}
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto hash_duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
for (int i = 0; i < N; ++i) {
|
||||
string &key = keys[i % length];
|
||||
|
||||
auto it = std::find_if(array.begin(), array.end(), [&key](const auto &pair) {
|
||||
return pair.first == key;
|
||||
});
|
||||
|
||||
volatile int value = it->second;
|
||||
(void)value;
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
auto array_duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
|
||||
std::cout << length << "," << hash_duration << "," << array_duration << "\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "elements,hash,array\n";
|
||||
|
||||
for (int length = 1; length <= 10; length++)
|
||||
test_array(length);
|
||||
for (int length = 10; length <= 100; length += 10)
|
||||
test_array(length);
|
||||
for (int length = 100; length <= 1000; length += 100)
|
||||
test_array(length);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
elements,hash,array
|
||||
1,53,36
|
||||
2,57,49
|
||||
3,83,65
|
||||
4,99,84
|
||||
5,117,99
|
||||
6,132,115
|
||||
7,145,128
|
||||
8,163,141
|
||||
9,174,156
|
||||
10,191,167
|
||||
10,190,169
|
||||
20,339,306
|
||||
30,74,442
|
||||
40,71,579
|
||||
50,78,719
|
||||
60,74,874
|
||||
70,75,1064
|
||||
80,83,1244
|
||||
90,83,1394
|
||||
100,85,1477
|
||||
100,86,1485
|
||||
200,80,2912
|
||||
300,82,4401
|
||||
400,82,5946
|
||||
500,83,7526
|
||||
600,84,9256
|
||||
700,86,10927
|
||||
800,88,12828
|
||||
900,89,14502
|
||||
1000,90,16302
|
||||
|
Binary file not shown.
|
After Width: | Height: | Size: 149 KiB |
@@ -0,0 +1,24 @@
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
df = pd.read_csv("benchmark.csv")
|
||||
|
||||
plt.figure(figsize=(10, 6))
|
||||
|
||||
plt.plot(df["elements"], df["hash"], marker='o', label="Hash map")
|
||||
plt.plot(df["elements"], df["array"], marker='o', label="Array")
|
||||
|
||||
# LOG SCALE
|
||||
plt.xscale("log")
|
||||
plt.yscale("log")
|
||||
|
||||
plt.xlabel("Number of elements (log scale)")
|
||||
plt.ylabel("Time (ms) (log scale)")
|
||||
plt.title("Array vs Hash Map Lookup Performance")
|
||||
|
||||
plt.grid(True, which="both", linestyle="--", alpha=0.5)
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
||||
|
||||
plt.savefig("benchmark.png", dpi=200, bbox_inches="tight")
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 145 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 142 KiB |
Reference in New Issue
Block a user