Commit aa66d3a5 authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: compilation of some unit tests

parent 92a22882
......@@ -35,12 +35,20 @@ Description
using namespace Foam;
template<class T>
Ostream& printInfo(Ostream& os, const HashTable<T, T, Hash<T>>& ht)
{
os << " (size " << ht.size() << " capacity " << ht.capacity() << ") ";
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
const label nLoops = 30;
const label nLoops = 300;
const label nBase = 100000;
const label nSize = nLoops * nBase;
......@@ -52,17 +60,18 @@ int main(int argc, char *argv[])
// StaticHashTable<label, label, Hash<label>> map(2 * nSize);
HashTable<label, label, Hash<label>> map(2 * nSize);
Info<< "Constructed map of size: " << nSize
<< " (size " << map.size() << " capacity " << map.capacity() << ") "
<< " " << timer.cpuTimeIncrement() << " s\n\n";
Info<< "Constructed map of size: " << nSize;
printInfo(Info, map);
Info<< timer.cpuTimeIncrement() << " s\n\n";
for (label i = 0; i < nSize; i++)
{
map.insert(i, i);
}
Info<< "Inserted " << nSize << " elements"
<< " (size " << map.size() << " capacity " << map.capacity() << ") "
<< timer.cpuTimeIncrement() << " s\n";
Info<< "Inserted " << nSize << " elements";
printInfo(Info, map);
Info<< timer.cpuTimeIncrement() << " s\n\n";
label elemI = 0;
for (label iLoop = 0; iLoop < nLoops; iLoop++)
......@@ -72,12 +81,14 @@ int main(int argc, char *argv[])
map.erase(elemI++);
}
map.shrink();
Info<< "loop " << iLoop << " - Erased " << nBase << " elements"
<< " (size " << map.size() << " capacity " << map.capacity() << ") "
<< timer.cpuTimeIncrement() << " s\n";
// map.shrink();
Info<< "loop " << iLoop << " - Erased " << nBase << " elements";
printInfo(Info, map);
Info << nl;
}
Info<< timer.cpuTimeIncrement() << " s\n";
return 0;
}
......
EXE_INC = ${c++LESSWARN}
/* EXE_LIBS = */
......@@ -34,9 +34,25 @@ Description
#include "StaticHashTable.H"
#include "cpuTime.H"
#include <vector>
#include <unordered_set>
using namespace Foam;
#undef TEST_STATIC_HASH
#undef TEST_STD_BOOLLIST
#undef TEST_STD_UNORDERED_SET
template<class T>
void printInfo(const std::unordered_set<T, Foam::Hash<T>>& ht)
{
Info<<"std::unordered_set elements:"
<< ht.size()
<< " buckets:" << ht.bucket_count()
<< " load_factor: " << ht.load_factor()
<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -47,11 +63,16 @@ int main(int argc, char *argv[])
const label n = 1000000;
const label nIters = 1000;
unsigned int sum = 0;
unsigned long sum = 0;
PackedBoolList packed(n, 1);
boolList unpacked(n, true);
std::vector<bool> stlVector(n, true);
#ifdef TEST_STD_BOOLLIST
std::vector<bool> stdBoolList(n, true);
#endif
cpuTime timer;
labelHashSet emptyHash;
labelHashSet fullHash(1024);
......@@ -60,6 +81,11 @@ int main(int argc, char *argv[])
fullHash.insert(i);
}
Info<< "populated labelHashSet in "
<< timer.cpuTimeIncrement() << " s\n\n";
#ifdef TEST_STATIC_HASH
// fullStaticHash is really slow
// give it lots of slots to help
StaticHashTable<nil, label, Hash<label>> emptyStaticHash;
......@@ -68,14 +94,32 @@ int main(int argc, char *argv[])
{
fullStaticHash.insert(i, nil());
}
Info<< "populated StaticHashTable in "
<< timer.cpuTimeIncrement() << " s\n\n";
#endif
#ifdef TEST_STD_UNORDERED_SET
std::unordered_set<label, Foam::Hash<label>> emptyStdHash;
std::unordered_set<label, Foam::Hash<label>> fullStdHash;
fullStdHash.reserve(1024);
for (label i = 0; i < n; i++)
{
fullStdHash.insert(i);
}
Info<< "populated unordered_set in "
<< timer.cpuTimeIncrement() << " s\n\n";
#endif
emptyHash.printInfo(Info);
fullHash.printInfo(Info);
#ifdef TEST_STATIC_HASH
emptyStaticHash.printInfo(Info);
fullStaticHash.printInfo(Info);
cpuTime timer;
#endif
#ifdef TEST_STD_UNORDERED_SET
printInfo(emptyStdHash);
printInfo(fullStdHash);
#endif
for (label iter = 0; iter < nIters; ++iter)
{
......@@ -104,9 +148,11 @@ int main(int argc, char *argv[])
sum += packed[i];
}
}
Info<< "Counting brute-force:" << timer.cpuTimeIncrement()
std::cout
<< "Counting brute-force:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Count packed
......@@ -115,9 +161,11 @@ int main(int argc, char *argv[])
{
sum += packed.count();
}
Info<< "Counting via count():" << timer.cpuTimeIncrement()
std::cout
<< "Counting via count():" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Dummy addition
......@@ -129,25 +177,29 @@ int main(int argc, char *argv[])
sum += i + 1;
}
}
Info<< "Dummy loop:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << " (sum is meaningless)" << endl;
std::cout
<< "Dummy loop:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << " (sum is meaningless)" << nl;
//
// Read
//
#ifdef TEST_STD_BOOLLIST
// Read stl
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
for (unsigned int i = 0; i < stlVector.size(); i++)
for (unsigned int i = 0; i < stdBoolList.size(); i++)
{
sum += stlVector[i];
sum += stdBoolList[i];
}
}
Info<< "Reading stl:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << endl;
std::cout
<< "Reading stdBoolList:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << nl;
#endif
// Read unpacked
sum = 0;
......@@ -158,8 +210,9 @@ int main(int argc, char *argv[])
sum += unpacked[i];
}
}
Info<< "Reading unpacked:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << endl;
std::cout
<< "Reading unpacked:" << timer.cpuTimeIncrement() << " s" << nl
<< " sum " << sum << nl;
// Read packed
......@@ -171,9 +224,10 @@ int main(int argc, char *argv[])
sum += packed.get(i);
}
}
Info<< "Reading packed using get:" << timer.cpuTimeIncrement()
std::cout
<< "Reading packed using get:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Read packed
......@@ -185,9 +239,10 @@ int main(int argc, char *argv[])
sum += packed[i];
}
}
Info<< "Reading packed using reference:" << timer.cpuTimeIncrement()
std::cout
<< "Reading packed using reference:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Read via iterator
......@@ -199,9 +254,10 @@ int main(int argc, char *argv[])
sum += it;
}
}
Info<< "Reading packed using iterator:" << timer.cpuTimeIncrement()
std::cout
<< "Reading packed using iterator:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Read via iterator
......@@ -213,9 +269,10 @@ int main(int argc, char *argv[])
sum += cit();
}
}
Info<< "Reading packed using const_iterator():" << timer.cpuTimeIncrement()
std::cout
<< "Reading packed using const_iterator():" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Read empty hash
......@@ -227,9 +284,10 @@ int main(int argc, char *argv[])
sum += emptyHash.found(i);
}
}
Info<< "Reading empty labelHashSet:" << timer.cpuTimeIncrement()
std::cout
<< "Reading empty labelHashSet:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Read full hash
......@@ -241,12 +299,14 @@ int main(int argc, char *argv[])
sum += fullHash.found(i);
}
}
Info<< "Reading full labelHashSet:" << timer.cpuTimeIncrement()
std::cout
<< "Reading full labelHashSet:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
// Read empty static hash
#ifdef TEST_STATIC_HASH
// Read empty StaticHashTable
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
......@@ -255,13 +315,12 @@ int main(int argc, char *argv[])
sum += emptyStaticHash.found(i);
}
}
Info<< "Reading empty StaticHash:" << timer.cpuTimeIncrement()
std::cout
<< "Reading empty StaticHash:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
<< " sum " << sum << nl;
#if 0
// we can skip this test - it is usually quite slow
// Read full static hash
// Read full StaticHashTable
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
......@@ -270,26 +329,60 @@ int main(int argc, char *argv[])
sum += fullStaticHash.found(i);
}
}
Info<< "Reading full StaticHash:" << timer.cpuTimeIncrement()
std::cout
<< "Reading full StaticHash:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << endl;
#endif
<< " sum " << sum << nl;
#endif
Info<< "Starting write tests" << endl;
#ifdef TEST_STD_UNORDERED_SET
// Read empty stl set
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += (emptyStdHash.find(i) != emptyStdHash.cend());
}
}
std::cout
<< "Reading empty std::unordered_set:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
// Read full stl set
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += (fullStdHash.find(i) != fullStdHash.cend());
}
}
std::cout
<< "Reading full std::unordered_set:" << timer.cpuTimeIncrement()
<< " s" << nl
<< " sum " << sum << nl;
#endif
Info<< "Starting write tests" << nl;
//
// Write
//
// Write stl
#ifdef TEST_STD_BOOLLIST
// Read stl
for (label iter = 0; iter < nIters; ++iter)
{
for (unsigned int i = 0; i < stlVector.size(); i++)
for (unsigned int i = 0; i < stdBoolList.size(); i++)
{
stlVector[i] = true;
stdBoolList[i] = true;
}
}
Info<< "Writing stl:" << timer.cpuTimeIncrement() << " s" << endl;
Info<< "Writing stdBoolList:" << timer.cpuTimeIncrement() << " s" << nl;
#endif
// Write unpacked
for (label iter = 0; iter < nIters; ++iter)
......@@ -299,7 +392,7 @@ int main(int argc, char *argv[])
unpacked[i] = true;
}
}
Info<< "Writing unpacked:" << timer.cpuTimeIncrement() << " s" << endl;
Info<< "Writing unpacked:" << timer.cpuTimeIncrement() << " s" << nl;
// Write packed
......@@ -311,7 +404,7 @@ int main(int argc, char *argv[])
}
}
Info<< "Writing packed using reference:" << timer.cpuTimeIncrement()
<< " s" << endl;
<< " s" << nl;
// Write packed
......@@ -323,7 +416,7 @@ int main(int argc, char *argv[])
}
}
Info<< "Writing packed using set:" << timer.cpuTimeIncrement()
<< " s" << endl;
<< " s" << nl;
// Write packed
for (label iter = 0; iter < nIters; ++iter)
......@@ -334,7 +427,7 @@ int main(int argc, char *argv[])
}
}
Info<< "Writing packed using iterator:" << timer.cpuTimeIncrement()
<< " s" << endl;
<< " s" << nl;
// Write packed
......@@ -343,7 +436,7 @@ int main(int argc, char *argv[])
packed = 0;
}
Info<< "Writing packed uniform 0:" << timer.cpuTimeIncrement()
<< " s" << endl;
<< " s" << nl;
// Write packed
......@@ -352,7 +445,7 @@ int main(int argc, char *argv[])
packed = 1;
}
Info<< "Writing packed uniform 1:" << timer.cpuTimeIncrement()
<< " s" << endl;
<< " s" << nl;
PackedList<3> oddPacked(n, 3);
......@@ -363,7 +456,7 @@ int main(int argc, char *argv[])
packed = 0;
}
Info<< "Writing packed<3> uniform 0:" << timer.cpuTimeIncrement()
<< " s" << endl;
<< " s" << nl;
// Write packed
......@@ -372,7 +465,7 @@ int main(int argc, char *argv[])
packed = 1;
}
Info<< "Writing packed<3> uniform 1:" << timer.cpuTimeIncrement()
<< " s" << endl;
<< " s" << nl;
Info<< "End\n" << endl;
......
......@@ -28,11 +28,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "boolList.H"
#include "HashSet.H"
#include "StaticHashTable.H"
#include "cpuTime.H"
#include <vector>
#include "PackedBoolList.H"
using namespace Foam;
......@@ -44,24 +40,29 @@ using namespace Foam;
int main(int argc, char *argv[])
{
const label nLoop = 5;
const label n = 100000000;
const label nReport = 1000000;
// const label nReport = 1000000;
cpuTime timer;
// test inserts
// PackedBoolList
PackedBoolList packed;
for (label i = 0; i < n; i++)
for (label iloop = 0; iloop < nLoop; ++iloop)
{
if ((i % nReport) == 0 && i)
for (label i = 0; i < n; ++i)
{
Info<< "i:" << i << " in " << timer.cpuTimeIncrement() << " s"
<<endl;
// if ((i % nReport) == 0 && i)
// {
// Info<< "." << flush;
// }
packed[i] = 1;
// Make compiler do something else too
packed[i/2] = 0;
}
packed[i] = 1;
}
Info<< "insert test: " << n << " elements in "
Info<< nl
<< "insert test: " << nLoop << "*" << n << " elements in "
<< timer.cpuTimeIncrement() << " s\n\n";
Info << "\nEnd\n" << endl;
......
......@@ -28,6 +28,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "uLabel.H"
#include "boolList.H"
#include "IOstreams.H"
#include "PackedBoolList.H"
#include "StringStream.H"
......@@ -146,17 +147,16 @@ int main(int argc, char *argv[])
Info<< list4 << " indices: " << list4.used()() << nl;
Info<< "\nassign from labelList\n";
list4 = labelList
(
IStringStream
(
"(0 1 2 3 12 13 14 19 20 21)"
)()
);
list4 = labelList{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() << nl;
// Not yet:
// PackedBoolList list5{0, 1, 2, 3, 12, 13, 14, 19, 20, 21};
// list5.printInfo(Info, true);
// Info<< list5 << " indices: " << list5.used()() << nl;
Info<< "\nassign from indices\n";
list4.read
(
......@@ -170,13 +170,13 @@ int main(int argc, char *argv[])
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() << nl;
List<bool> boolLst(list4.size());
boolList bools(list4.size());
forAll(list4, i)
{
boolLst[i] = list4[i];
bools[i] = list4[i];
}
Info<< "List<bool>: " << boolLst << nl;
Info<< "boolList: " << bools << nl;
// check roundabout assignments
PackedList<2> pl2
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment