Skip to content

Commit 2ee5761

Browse files
committed
Add weighted average to SpeedSmall test output
It addresses the question at rurban#113 What is the "real" average cycles/hash value for a given hash function? We can't know, but we can estimate it better if we assume that the function timing does not depend on input (that's not true for hashes based on multiplication) and we know distribution of key length in advance (that might be somewhat known for certain classes of inputs, but the distribution varies across classes measurably).
1 parent 0c6a1ef commit 2ee5761

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

Platform.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
#include "Platform.h"
22

33
#include <stdio.h>
4+
#include <assert.h>
45

5-
void testRDTSC ( void )
6+
long getenvlong(const char *name, long minval, long defval, long maxval)
67
{
7-
int64_t temp = rdtsc();
8-
printf("%ld",(long)temp);
8+
assert(minval <= defval && defval <= maxval);
9+
const char *s = getenv(name);
10+
if (!s)
11+
return defval;
12+
char *tail;
13+
long l = strtol(s, &tail, 0);
14+
if (*tail)
15+
return defval;
16+
if (l < minval) l = minval;
17+
if (l > maxval) l = maxval;
18+
return l;
919
}
1020

1121
#if defined(_WIN32)

Platform.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void SetThreadAffinity ( std::thread &t, int cpu );
2121
# endif
2222
#endif
2323
void SetAffinity ( int cpu );
24+
long getenvlong(const char *name, long minval, long defval, long maxval);
2425

2526
// That's not UINT64_MAX as it's converted to int64_t sometimes.
2627
constexpr uint64_t timer_inf = INT64_MAX;

SpeedTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ double SpeedTest ( pfHash hash, uint32_t seed, const int trials, const int block
240240

241241
double t;
242242

243-
if(blocksize < 100)
243+
if(blocksize <= TIMEHASH_SMALL_LEN_MAX)
244244
{
245245
t = (double)timehash_small(hash,block,blocksize,itrial);
246246
}

SpeedTest.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "Types.h"
44

5+
constexpr int TIMEHASH_SMALL_LEN_MAX = 255;
6+
57
void BulkSpeedTest ( pfHash hash, uint32_t seed );
68
double TinySpeedTest ( pfHash hash, int hashsize, int keysize, uint32_t seed, bool verbose );
79
double HashMapSpeedTest ( pfHash pfhash, int hashbits, std::vector<std::string> words,

main.cpp

+47-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <chrono>
1313
#endif
1414

15+
#include <sstream>
1516
#include <stdio.h>
1617
#include <stdint.h>
1718
#include <time.h>
@@ -86,6 +87,8 @@ bool MomentChi2Test ( struct HashInfo *info, int inputSize );
8687
//-----------------------------------------------------------------------------
8788
// This is the list of all hashes that SMHasher can test.
8889

90+
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
91+
8992
const char* quality_str[3] = { "SKIP", "POOR", "GOOD" };
9093

9194
// sorted by quality and speed. the last is the list of internal secrets to be tested against bad seeds.
@@ -979,6 +982,17 @@ void SelfTest(bool verbose) {
979982

980983
//----------------------------------------------------------------------------
981984

985+
static std::pair<double, double> CalcWAvg(const double *x, const double *dist, size_t imin, size_t imax)
986+
{
987+
double sum = 0, prb = 0;
988+
for (size_t i = imin; i <= imax; i++) {
989+
sum += dist[i] * x[i];
990+
prb += dist[i];
991+
}
992+
sum /= prb;
993+
return std::make_pair(sum, prb);
994+
}
995+
982996
template < typename hashtype >
983997
void test ( hashfunc<hashtype> hash, HashInfo* info )
984998
{
@@ -1027,7 +1041,6 @@ void test ( hashfunc<hashtype> hash, HashInfo* info )
10271041

10281042
if(g_testSpeedBulk || g_testSpeedSmall || g_testAll)
10291043
{
1030-
double sum = 0.0;
10311044
printf("[[[ Speed Tests ]]]\n\n");
10321045
if (timer_counts_ns())
10331046
printf("WARNING: no cycle counter, cycle == 1ns\n");
@@ -1050,17 +1063,43 @@ void test ( hashfunc<hashtype> hash, HashInfo* info )
10501063
}
10511064

10521065
if (g_testSpeedSmall || g_testAll) {
1053-
const char* const envsmin = getenv("SMHASHER_SMALLKEY_MIN");
1054-
const char* const envsmax = getenv("SMHASHER_SMALLKEY_MAX");
1055-
const int minkey = max(min(envsmin ? atoi(envsmin) : 1, 255), 1);
1056-
const int maxkey = max(min(envsmax ? atoi(envsmax) : 32, 255), minkey);
1066+
const int dflmax = g_testExtra ? 64 : 32;
1067+
const int minkey = getenvlong("SMHASHER_SMALLKEY_MIN", 1, 1, TIMEHASH_SMALL_LEN_MAX);
1068+
const int maxkey = getenvlong("SMHASHER_SMALLKEY_MAX", minkey, dflmax, TIMEHASH_SMALL_LEN_MAX);
1069+
double cph[TIMEHASH_SMALL_LEN_MAX + 1];
10571070
for(int i = minkey; i <= maxkey; i++)
10581071
{
10591072
volatile int j = i;
1060-
sum += TinySpeedTest(hashfunc<hashtype>(info->hash),sizeof(hashtype),j,info->verification,true);
1073+
cph[j] = TinySpeedTest(hashfunc<hashtype>(info->hash),sizeof(hashtype),j,info->verification,true);
1074+
}
1075+
{
1076+
double sum = 0;
1077+
for (int i = minkey; i <= maxkey; i++)
1078+
sum += cph[i];
1079+
sum /= (maxkey - minkey + 1);
1080+
g_speed = sum;
1081+
printf("Average %8.3f cycles/hash\n",sum);
1082+
}
1083+
if (const char *weights = getenv("SMHASHER_SMALLKEY_WEIGHTS"))
1084+
{
1085+
// Weighted average exist under assumption that hash speed does not depend on input,
1086+
// which is not true due to multiplication instruction having certain amount of variance.
1087+
std::vector<double> lenprob;
1088+
std::istringstream ssws(weights);
1089+
double sum = 0.0;
1090+
for (double f; ssws >> f; ) {
1091+
lenprob.push_back(f);
1092+
sum += f;
1093+
}
1094+
for (size_t i = 0; i < lenprob.size(); i++)
1095+
lenprob[i] /= sum;
1096+
if (maxkey < lenprob.size()) {
1097+
const auto m = CalcWAvg(cph, lenprob.data(), minkey, maxkey);
1098+
printf("Weighted average, %5.1f%% of weights %8.3f cycles/hash\n",100.*m.second,m.first);
1099+
} else {
1100+
printf("Weighted average needs %u more weights (SKIP it)\n", (unsigned)(maxkey - lenprob.size() + 1));
1101+
}
10611102
}
1062-
g_speed = sum = sum / (maxkey - minkey + 1);
1063-
printf("Average %6.3f cycles/hash\n",sum);
10641103
printf("\n");
10651104
fflush(NULL);
10661105
}

0 commit comments

Comments
 (0)