Test-HashTable3.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  Test speeds for some HashTable operations
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "argList.H"
30 #include "HashTable.H"
31 #include "HashPtrTable.H"
32 #include "Map.H"
33 #include "StaticHashTable.H"
34 #include "cpuTime.H"
35 
36 using namespace Foam;
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 // Main program:
40 
41 int main(int argc, char *argv[])
42 {
43  const label nLoops = 30;
44  const label nBase = 100000;
45  const label nSize = nLoops * nBase;
46 
47  cpuTime timer;
48 
49  // ie, a
50  // Map<label> map(2 * nSize);
51  // HashTable<label, label, Hash<label> > map(2 * nSize);
52  // StaticHashTable<label, label, Hash<label> > map(2 * nSize);
53  HashTable<label, label, Hash<label> > map(2 * nSize);
54 
55  Info<< "Constructed map of size: " << nSize
56  << " (size " << map.size() << " capacity " << map.capacity() << ") "
57  << " " << timer.cpuTimeIncrement() << " s\n\n";
58 
59  for (label i = 0; i < nSize; i++)
60  {
61  map.insert(i, i);
62  }
63  Info<< "Inserted " << nSize << " elements"
64  << " (size " << map.size() << " capacity " << map.capacity() << ") "
65  << timer.cpuTimeIncrement() << " s\n";
66 
67  label elemI = 0;
68  for (label iLoop = 0; iLoop < nLoops; iLoop++)
69  {
70  for (label i = 0; i < nBase; i++)
71  {
72  map.erase(elemI++);
73  }
74 
75  map.shrink();
76  Info<< "loop " << iLoop << " - Erased " << nBase << " elements"
77  << " (size " << map.size() << " capacity " << map.capacity() << ") "
78  << timer.cpuTimeIncrement() << " s\n";
79  }
80 
81  return 0;
82 }
83 
84 // ************************************************************************* //
Foam::cpuTime
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:52
HashTable.H
Foam::HashTable::shrink
void shrink()
Shrink the allocated table to approx. twice number of elements.
Definition: HashTable.C:505
Foam::HashTable::capacity
label capacity() const
The size of the underlying table.
Definition: HashTableI.H:58
Foam::HashTable::insert
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
main
int main(int argc, char *argv[])
Definition: Test-HashTable3.C:41
StaticHashTable.H
Map.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Info
messageStream Info
argList.H
Foam::HashTable::erase
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Foam::HashTable::size
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
cpuTime.H
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
Foam::timer
Implements a timeout mechanism via sigalarm.
Definition: timer.H:81
HashPtrTable.H