From: hama Date: Mon, 19 Jan 2015 23:39:34 +0000 (+0100) Subject: mass test HashList, ReShiftRandom X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=f124d9493b6b8e69726542162909445aac64aa6e;p=crepublib mass test HashList, ReShiftRandom --- diff --git a/base/ReSeqArray.hpp b/base/ReSeqArray.hpp index 8e84fb1..b7a769d 100644 --- a/base/ReSeqArray.hpp +++ b/base/ReSeqArray.hpp @@ -30,7 +30,7 @@ public: Tag m_tag; } Sequence; public: - ReSeqArray(size_t deltaList = 128, int deltaBuffer = 1024); + ReSeqArray(size_t deltaList = -1024*sizeof(size_t), int deltaBuffer = - 1024*1024); virtual ~ReSeqArray(); ReSeqArray(const ReSeqArray& source); ReSeqArray& operator = (const ReSeqArray& source); diff --git a/cunit/cuReHashList.cpp b/cunit/cuReHashList.cpp index 3df7c16..33c7b86 100644 --- a/cunit/cuReHashList.cpp +++ b/cunit/cuReHashList.cpp @@ -8,14 +8,48 @@ public: } private: void run(){ + massTest(); testBasic(); testNext(); } void massTest(){ - ReCongruentialGenerator rand; - ReHashList hash(); + const int maxKeys = 50*1000; + const int minLength = 3; + const int maxLength = 10; + const int countGet = maxKeys * 50; + ReShiftRandom rand; + ReHashList hash(false, 3, 3, 1); + ReSeqArray list; + list.setCapacity(maxKeys, maxKeys * maxLength); + list.setSizes(4, maxKeys <= 0xffff ? 2 : 3); ReByteBuffer key, value; - log(false, "missing masstest"); + int64_t start = timer(); + ReSeqArray::Tag tag; + int ix; + int collisions = 0; + for(ix = 0; ix < maxKeys; ix++){ + rand.nextString(minLength, maxLength, key); + if (hash.get(key, value)){ + value.append("+", 1); + collisions++; + } else { + value.set(key).append("X", 1); + } + hash.put(key, value); + } + int duration1 = milliSecSince(start); + start = timer(); + for (ix = 0; ix < countGet; ix++){ + int ix2 = rand.nextInt(maxKeys); + list.get(ix2, key); + checkT(hash.get(key, value)); + checkT(value.startsWith(key.str(), key.length())); + } + int duration2 = milliSecSince(start); + logF(false, "hashlist: keys: %1d duration: %s get count: %d duration: %s collisions: %d", + maxKeys, key.setLength(0).appendMilliSec(duration1).str(), + countGet, value.setLength(0).appendMilliSec(duration2).str(), + collisions); } void testBasic(){ ReHashList hash; diff --git a/cunit/testall.cpp b/cunit/testall.cpp index 246b62f..f914f47 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -11,6 +11,8 @@ #endif void testBase(){ + extern void testReHashList(void); + testReHashList(); extern void testReTestUnit(); testReTestUnit(); @@ -64,8 +66,8 @@ void testMath(){ void testAll(){ try { - testOs(); testBase(); + testOs(); testMath(); testString(); } catch (ReException e){ diff --git a/math/ReRandomizer.cpp b/math/ReRandomizer.cpp index 233c500..d646d92 100644 --- a/math/ReRandomizer.cpp +++ b/math/ReRandomizer.cpp @@ -2,7 +2,6 @@ * ReRandomizer.cpp * * Created on: 01.11.2010 - * Author: wk */ @@ -80,9 +79,12 @@ int ReRandomizer::nextInt(int maxValue, int minValue){ seed_t seed = nextSeed(); if (minValue == maxValue) rc = minValue; - else - // we calculate in 64 bit: - rc = (int) (minValue + seed % (maxValue - minValue + 1)); + else { + // we calculate in 64 bit, no range overflow: + int64_t range = (int64_t) maxValue - minValue + 1; + int64_t offset = seed % range; + rc = (int) (minValue + offset); + } if (s_trace){ static int count = 0; printf ("%c %8x ", count++ % 4 == 0 ? '\n' : ' ', rc); @@ -141,19 +143,20 @@ ReRandomizer::seed_t ReCongruentialGenerator::nextSeed(){ * * All character will be inside the range ' ' .. chr(127). * - * @param buffer Out: The place for the string. - * @param maxLength The maximum length of the result string. * @param minLength The minimum length of the result string. + * @param maxLength The maximum length of the result string. + * @param buffer Out: The place for the string. * * @result The buffer. */ -char* ReRandomizer::nextString(char* buffer, int maxLength, int minLength){ - int len = nextInt(minLength, maxLength); +const char* ReRandomizer::nextString(int minLength, int maxLength, ReByteBuffer &buffer){ + int len = nextInt(maxLength, minLength); + buffer.setLength(len); + char* ptr = buffer.buffer(); for (int ii = 0; ii < len; ii++){ - buffer[ii] = nextChar(); + ptr[ii] = nextChar(); } - buffer[len] = '\0'; - return buffer; + return buffer.str(); } /** @@ -216,8 +219,8 @@ void ReRandomizer::shuffle(void* array, size_t length, size_t elemSize){ */ ReCongruentialGenerator::ReCongruentialGenerator() : m_seed(0x4711), - m_factor(2631), - m_increment(0x9), + m_factor(214013), + m_increment(2531011), m_lastSetSeed(0x4711) { } @@ -297,3 +300,17 @@ void ReCongruentialGenerator::setSeed(seed_t seed) printf(" Seed: %llx ", seed); } + +/** @brief Returns the next 64 bit pseudo random number. + * + * A congruential generator produces good random in the most significant + * bits. Therefore we exchange the bits of the result. + * Then x % m returns better results. + * + * @return a pseudo random number +*/ +ReRandomizer::seed_t ReShiftRandom::nextSeed(){ + seed_t rc = ReCongruentialGenerator::nextSeed(); + rc = ((rc & 0x7fffffff) << 33) | ((rc >> 31) & 0x1ffffffffll); + return rc; +} diff --git a/math/ReRandomizer.hpp b/math/ReRandomizer.hpp index 32f0812..63bd3c8 100644 --- a/math/ReRandomizer.hpp +++ b/math/ReRandomizer.hpp @@ -2,7 +2,6 @@ * ReRandomizer.h * * Created on: 01.11.2010 - * Author: wk */ #ifndef RANDOMIZER_H_ @@ -23,7 +22,7 @@ public: virtual int nextInt(int maxValue = INT_MAX, int minValue = 0); virtual int64_t nextInt64(int64_t maxValue = LLONG_MAX, int64_t minValue = 0); char nextChar(); - char* nextString(char* buffer, int maxLength = 80, int minLength = 0); + const char* nextString(int minLength, int maxLength, ReByteBuffer& buffer); void shuffle(void* array, size_t length, size_t elemSize); /** @brief Sets the instance to a defined start state. */ @@ -55,7 +54,8 @@ public: void setFactor(seed_t factor); void setIncrement(seed_t increment); void setSeed(seed_t m_seed); -private: +protected: + friend class ReShiftRandom; virtual seed_t nextSeed(); private: seed_t m_seed; @@ -64,4 +64,8 @@ private: seed_t m_lastSetSeed; }; +class ReShiftRandom : public ReCongruentialGenerator { +protected: + virtual seed_t nextSeed(); +}; #endif /* RANDOMIZER_H_ */