Bachelors_Thesis_Code/fagprojekt_code/eq17.cpp

139 lines
5.8 KiB
C++
Raw Normal View History

2021-11-14 14:35:05 +01:00
#include "hashing_algorithms.hpp"
#include <stdlib.h>
#include <chrono>
#include <iostream>
/* #include <stdio.h> */
#include <cmath>
/* #include <string.h> */
/* #include <stdint.h> */
using namespace std;
int main() {
unsigned int M = 9299983;
unsigned int m = 3226097;
unsigned int result;
const clock_t begin_time1 = clock();
for (unsigned int y = 0; y < M; y++) {
result = (unsigned int) ((float)(y*m)/(float)M);
}
const clock_t end_time1 = clock();
cout << "Naive time: " << float( end_time1 - begin_time1 ) / CLOCKS_PER_SEC << endl;
cout << "Result: " << result << endl;
const clock_t begin_time2 = clock();
for (unsigned int y = 0; y < M; y += 10) {
result = (unsigned int) ((float)(y*m)/(float)M);
result = (unsigned int) ((float)((y+1)*m)/(float)M);
result = (unsigned int) ((float)((y+2)*m)/(float)M);
result = (unsigned int) ((float)((y+3)*m)/(float)M);
result = (unsigned int) ((float)((y+4)*m)/(float)M);
result = (unsigned int) ((float)((y+5)*m)/(float)M);
result = (unsigned int) ((float)((y+6)*m)/(float)M);
result = (unsigned int) ((float)((y+7)*m)/(float)M);
result = (unsigned int) ((float)((y+8)*m)/(float)M);
result = (unsigned int) ((float)((y+9)*m)/(float)M);
}
const clock_t end_time2 = clock();
cout << "Naive time (unrolled): " << float( end_time2 - begin_time2 ) / CLOCKS_PER_SEC << endl;
cout << "Result: " << result << endl;
const clock_t begin_time6 = clock();
for (unsigned int y = 0; y < M; y++) {
result = y*m/M;
}
const clock_t end_time6 = clock();
cout << "Naive time (no-typecasting): " << float( end_time6 - begin_time6 ) / CLOCKS_PER_SEC << endl;
cout << "Result: " << result << endl;
const clock_t begin_time7 = clock();
for (unsigned int y = 0; y < M; y += 10) {
result = y*m/M;
result = ((y+1)*m)/M;
result = ((y+2)*m)/M;
result = ((y+3)*m)/M;
result = ((y+4)*m)/M;
result = ((y+5)*m)/M;
result = ((y+6)*m)/M;
result = ((y+7)*m)/M;
result = ((y+8)*m)/M;
result = ((y+9)*m)/M;
}
const clock_t end_time7 = clock();
cout << "Naive time (no-typecasting, unrolled): " << float( end_time7 - begin_time7 ) / CLOCKS_PER_SEC << endl;
cout << "Result: " << result << endl;
float mM = (float) m/(float) M;
const clock_t begin_time3 = clock();
for (unsigned int y = 0; y < M; y ++) {
result = (unsigned int) (((float) y)*mM);
}
const clock_t end_time3 = clock();
cout << "Precalculation time: " << float( end_time3 - begin_time3 ) / CLOCKS_PER_SEC << endl;
cout << "Result: " << result << endl;
const clock_t begin_time4 = clock();
for (unsigned int y = 0; y < M; y += 10) {
result = (unsigned int) ((float) y*mM);
result = (unsigned int) ((float) (y+1)*mM);
result = (unsigned int) ((float) (y+2)*mM);
result = (unsigned int) ((float) (y+3)*mM);
result = (unsigned int) ((float) (y+4)*mM);
result = (unsigned int) ((float) (y+5)*mM);
result = (unsigned int) ((float) (y+6)*mM);
result = (unsigned int) ((float) (y+7)*mM);
result = (unsigned int) ((float) (y+8)*mM);
result = (unsigned int) ((float) (y+9)*mM);
}
const clock_t end_time4 = clock();
cout << "Precalculation time (unrolled): " << float( end_time4 - begin_time4 ) / CLOCKS_PER_SEC << endl;
cout << "Result: " << result << endl;
/* for (unsigned int y = 0; y < M; y++){ */
/* // Very often, differentiating by multiplying first or dividing first results in the result being off by 1 from each other */
/* /1* if (abs((int) ((float)y*((float)m/(float)M)) - (int) ((float) y*mM)) >= 1) *1/ */
/* /1* cout <<(unsigned int) ((float)y*(float)m/(float)M) << " != " << (unsigned int) ((float) y*mM) << endl; *1/ */
/* /1* if (abs((int)(y*m/M - (y*m)/M)) >= 1) *1/ */
/* /1* cout <<y*m/M << " != " << (y*m)/M << endl; *1/ */
/* /1* if (abs((int)(((float)y*((float)m/(float)M)) - (y*m)/M)) >= 1) *1/ */
/* /1* cout <<(unsigned int)((float)y*((float)m/(float)M)) << " != " << (y*m)/M << endl; *1/ */
/* if (abs((int)(((float)y*(float)m)/(float)M - (unsigned int)y*(unsigned int)m/(unsigned int)M)) >= 1) */
/* cout <<((float)(y*m)/(float)M << " != " << (unsigned int)y*(unsigned int)m/(unsigned int)M << endl; */
/* } */
// The non-precalculation model is susceptible to overflow
/* for (unsigned int y = 0; y < 40; y++) { */
/* cout << "y: " << y << ", "; */
/* cout << "m: " << m << ", "; */
/* cout << "M: " << M << ", "; */
/* cout << "mM: " << mM << ", "; */
/* cout << "y*m: " << y*m << ", "; */
/* cout << "(y*m)/M: " << (y*m)/M << ", "; */
/* cout << "y*(m/M): " << y*(m/M) << ", "; */
/* cout << y*m/M; */
/* cout << " != "; */
/* cout << (unsigned int) ((float)(y*m)/(float)M); */
/* cout << " != "; */
/* cout << (unsigned int) ((float)y*(float)m/(float)M); */
/* cout << " != "; */
/* cout << (unsigned int) ((float) y*mM); */
/* cout << endl; */
/* } */
uint64_t result_un;
uint64_t p = pow(2, 61)-1;
uint64_t m_un = pow(2, 20)+2;
const clock_t begin_time_un = clock();
for (uint64_t y = 0; y < 100000000; y++) {
/* result_un = multiply_mod_prime_mersenne_overflow(y, m, M, p, m_un); */
result_un = multiply_shift_strongly_universal(y, p, m_un, 20);
}
const clock_t end_time_un = clock();
cout << "Unrelated hash function time: " << float( end_time_un - begin_time_un ) / CLOCKS_PER_SEC << endl;
cout << "Result: " << result_un << endl;
return EXIT_SUCCESS;
}