Skip to content
Snippets Groups Projects
Commit 290a506e authored by David Nieder's avatar David Nieder
Browse files

benchmark program

parent b56a1130
No related branches found
No related tags found
No related merge requests found
add_library(hashtable STATIC hashtable.cpp) add_library(hashtable STATIC hashtable.cpp)
add_executable(main main.cpp) add_executable(benchmark benchmark.cpp)
target_link_libraries(main hashtable) target_link_libraries(benchmark hashtable)
#include <chrono>
#include <string>
#include <utility>
#include <cstdlib>
#include <iostream>
#include <exception>
#include "hashtable.h"
using std::cout;
using std::cerr;
using std::endl;
using std::pair;
using std::string;
using std::chrono::steady_clock;
using duration = std::chrono::duration<double>;
const unsigned runs = 10;
const unsigned rseed = 12975033;
void print_usage(const string &pname) {
cerr << "Usage: " << pname << " N" << endl;
cerr << "Where N is an integer greater than zero." << endl;
}
template <typename Tabletype>
pair<double,string> benchmark_linearinsertion(unsigned n, float alpha, float s) {
duration measurement = duration::zero();
string tableinfo;
for (unsigned i=0; i<runs; i++) {
auto start = steady_clock::now();
Tabletype hashtable(0, alpha, s);
for (unsigned j=1; j<=n; j++) {
hashtable.insert(j);
}
measurement += steady_clock::now() - start;
tableinfo = hashtable.info();
}
return { measurement.count()/runs, tableinfo };
}
int main(int argc, char *argv[])
{
long n;
const string pname(argv[0]);
pair<double,string> result;
if (argc != 2) {
print_usage(pname);
return 1;
}
if (argc == 2 && (string("-h") == argv[1] || string("--help") == argv[1])) {
print_usage(pname);
return 1;
}
try { n = std::stol(argv[1]); }
catch (const std::exception &ex) {
print_usage(pname);
return 1;
}
if (n <= 0) {
print_usage(pname);
return 1;
}
cout << "Inserting integers from 1 to " << n << " into different hashtables." << endl;
cout << "All measurements are averages over " << runs << " runs." << endl << endl;
cout << "Table: hashing by division, linear probing, alpha: " << 0.5 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,LinearProbing>>(n, 0.5, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, linear probing, alpha: " << 0.5 << ", s: " << 4 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,LinearProbing>>(n, 0.5, 4);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, linear probing, alpha: " << 0.75 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,LinearProbing>>(n, 0.75, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, quadratic probing, alpha: " << 0.5 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,QuadraticProbing>>(n, 0.5, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, quadratic probing, alpha: " << 0.5 << ", s: " << 4 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,QuadraticProbing>>(n, 0.5, 4);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, quadratic probing, alpha: " << 0.75 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,QuadraticProbing>>(n, 0.75, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, double hashing, alpha: " << 0.5 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,DoubleHashing>>(n, 0.5, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, double hashing, alpha: " << 0.5 << ", s: " << 4 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,DoubleHashing>>(n, 0.5, 4);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by division, double hashing, alpha: " << 0.75 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntDivHashing,DoubleHashing>>(n, 0.75, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, linear probing, alpha: " << 0.5 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,LinearProbing>>(n, 0.5, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, linear probing, alpha: " << 0.5 << ", s: " << 4 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,LinearProbing>>(n, 0.5, 4);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, linear probing, alpha: " << 0.75 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,LinearProbing>>(n, 0.75, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, quadratic probing, alpha: " << 0.5 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,QuadraticProbing>>(n, 0.5, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, quadratic probing, alpha: " << 0.5 << ", s: " << 4 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,QuadraticProbing>>(n, 0.5, 4);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, quadratic probing, alpha: " << 0.75 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,QuadraticProbing>>(n, 0.75, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, double hashing, alpha: " << 0.5 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,DoubleHashing>>(n, 0.5, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, double hashing, alpha: " << 0.5 << ", s: " << 4 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,DoubleHashing>>(n, 0.5, 4);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
cout << "Table: hashing by multiplication, double hashing, alpha: " << 0.75 << ", s: " << 2 << endl;;
result = benchmark_linearinsertion<IntegerTable<IntMulHashing,DoubleHashing>>(n, 0.75, 2);
cout << "Time: " << result.first << "s, " << result.second << endl << endl;
return 0;
}
...@@ -16,6 +16,8 @@ class DivHashing { ...@@ -16,6 +16,8 @@ class DivHashing {
size_t m; size_t m;
}; };
using IntDivHashing = DivHashing<unsigned int>;
/* /*
* hashing by multiplication * hashing by multiplication
*/ */
...@@ -29,6 +31,9 @@ class MulHashing { ...@@ -29,6 +31,9 @@ class MulHashing {
size_t m; size_t m;
}; };
using IntMulHashing = MulHashing<unsigned int>;
/* /*
* linear probing * linear probing
*/ */
......
...@@ -56,4 +56,7 @@ template class Hashtable<unsigned int, MulHashing<unsigned int>, LinearProbing>; ...@@ -56,4 +56,7 @@ template class Hashtable<unsigned int, MulHashing<unsigned int>, LinearProbing>;
template class Hashtable<unsigned int, MulHashing<unsigned int>, QuadraticProbing>; template class Hashtable<unsigned int, MulHashing<unsigned int>, QuadraticProbing>;
template class Hashtable<unsigned int, MulHashing<unsigned int>, DoubleHashing>; template class Hashtable<unsigned int, MulHashing<unsigned int>, DoubleHashing>;
template <typename H, typename P> using IntegerTable = Hashtable<unsigned, H, P>;
#endif #endif
#include <iostream>
int main() {
std::cout << "Hello CMake." << std::endl;
}
...@@ -5,16 +5,13 @@ add_subdirectory(/usr/src/gtest ${PROJECT_BINARY_DIR}/gtest) ...@@ -5,16 +5,13 @@ add_subdirectory(/usr/src/gtest ${PROJECT_BINARY_DIR}/gtest)
include(CTest) include(CTest)
# the executables that run the tests # the executables that run the tests
add_executable(test_main test_main.cpp)
add_executable(test_hashing test_hashing.cpp) add_executable(test_hashing test_hashing.cpp)
add_executable(test_hashtable test_constructor.cpp test_hashtable.cpp) add_executable(test_hashtable test_constructor.cpp test_hashtable.cpp)
# link needed libraries # link needed libraries
target_link_libraries(test_main gtest gtest_main)
target_link_libraries(test_hashing gtest gtest_main) target_link_libraries(test_hashing gtest gtest_main)
target_link_libraries(test_hashtable hashtable gtest gtest_main) target_link_libraries(test_hashtable hashtable gtest gtest_main)
# run with ctest # run with ctest
add_test(NAME test_main COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_main)
add_test(NAME test_hashing COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_hashing) add_test(NAME test_hashing COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_hashing)
add_test(NAME test_hashtable COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_hashtable) add_test(NAME test_hashtable COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_hashtable)
#include <gtest/gtest.h>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment