d168a7633f39a737197f549d471bbc8f01e36906
[occt.git] / src / QANCollection / QANCollection_Simple.cxx
1 // Created on: 2004-03-05
2 // Created by: Mikhail KUZMITCHEV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <QANCollection.hxx>
17 #include <Draw_Interpretor.hxx>
18
19 #include <OSD_PerfMeter.hxx>
20
21 #include <TColgp_Array1OfPnt.hxx>
22 #include <TColgp_SequenceOfPnt.hxx>
23
24 #include <NCollection_Sequence.hxx>
25 #include <NCollection_Array1.hxx>
26 #include <NCollection_IncAllocator.hxx>
27
28 typedef NCollection_Array1<gp_Pnt> MyArray1;
29 typedef NCollection_Sequence<gp_Pnt> MySequence;
30
31 const Standard_Integer REPEAT = 100;
32
33 static void printAllMeters (Draw_Interpretor& theDI)
34 {
35   char buffer[25600];
36   perf_sprint_all_meters (buffer, 25600 - 1, 1);
37   theDI << buffer;
38 }
39
40 static void createArray (TColgp_Array1OfPnt& anArrPnt)
41 {
42   OSD_PerfMeter aPerfMeter("Create array");
43
44   for (Standard_Integer j = 0; j < 2*REPEAT; j++) {
45     PERF_START_METER("Create array")
46       for (Standard_Integer i = anArrPnt.Lower(); i <= anArrPnt.Upper(); i++)
47         anArrPnt(i).SetCoord ((double)i, (double)(i+1), (double)(i+2));
48     PERF_STOP_METER("Create array")
49   }
50 }
51
52 static void createSequence (TColgp_SequenceOfPnt& aSeq)
53 {
54   for (Standard_Integer j = 0; j < REPEAT; j++) {
55     PERF_START_METER("Clear sequence")
56     aSeq.Clear();
57     PERF_STOP_METER("Clear sequence")
58     PERF_START_METER("Create sequence")
59     for (Standard_Integer i = 0; i < 100000; i++)
60       aSeq.Append (gp_Pnt((double)i, (double)(i+1), (double)(i+2)));
61     PERF_STOP_METER("Create sequence")
62   }
63 }
64
65 static void createSequence (MySequence& aSeq)
66 {
67   for (Standard_Integer j = 0; j < REPEAT; j++) {
68     PERF_START_METER("Clear sequence")
69     aSeq.Clear();
70     PERF_STOP_METER("Clear sequence")
71     PERF_START_METER("Create sequence")
72     for (Standard_Integer i = 0; i < 100000; i++)
73       aSeq.Append (gp_Pnt((double)i, (double)(i+1), (double)(i+2)));
74     PERF_STOP_METER("Create sequence")
75   }
76 }
77
78 static void assignSequence (TColgp_SequenceOfPnt& aDest,
79                             const TColgp_SequenceOfPnt& aSrc)
80 {
81   for (Standard_Integer i = 0; i < REPEAT; i++) {
82     PERF_START_METER("Assign sequence to sequence")
83     aDest = aSrc;
84     PERF_STOP_METER("Assign sequence to sequence")
85   }
86 }
87
88 static void assignSequence (MySequence& aDest, const MySequence& aSrc)
89 {
90   for (Standard_Integer i = 0; i < REPEAT; i++) {
91     PERF_START_METER("Assign sequence to sequence")
92     aDest = aSrc;
93     PERF_STOP_METER("Assign sequence to sequence")
94   }
95 }
96
97 static void createArray (MyArray1& anArrPnt)
98 {
99   for (Standard_Integer j = 0; j < 2*REPEAT; j++) {
100     PERF_START_METER("Create array")
101       for (Standard_Integer i = anArrPnt.Lower(); i <= anArrPnt.Upper(); i++)
102         anArrPnt(i).SetCoord ((double)i, (double)(i+1), (double)(i+2));
103     PERF_STOP_METER("Create array")
104   }
105 }
106
107 static void assignArray (TColgp_Array1OfPnt& aDest, const TColgp_Array1OfPnt& aSrc)
108 {
109   for (Standard_Integer i = 0; i < 2*REPEAT; i++) {
110     PERF_START_METER("Assign array to array")
111     aDest = aSrc;
112     PERF_STOP_METER("Assign array to array")
113   }
114 }
115
116 template <class MyBaseCollPnt>
117 void assignCollection (MyBaseCollPnt&           aDest,
118                        const MyBaseCollPnt&     aSrc,
119                        const char               * MeterName)
120 {
121   for (Standard_Integer i = 0; i < REPEAT; i++) {
122     perf_start_meter (MeterName);
123     aDest.Assign(aSrc);
124     perf_stop_meter (MeterName);
125   }
126 }
127
128 static void assignArray (MyArray1& aDest, const MyArray1& aSrc)
129 {
130   for (Standard_Integer i = 0; i < 2*REPEAT; i++) {
131     PERF_START_METER("Assign array to array")
132     aDest = aSrc;
133     PERF_STOP_METER("Assign array to array")
134   }
135 }
136
137 static void checkArray (Draw_Interpretor& theDI, const Standard_Boolean isNewColl)
138 {
139   if (isNewColl) {
140     MyArray1 anArrPnt (1, 100000), anArrPnt1 (1, 100000);
141     createArray (anArrPnt);
142     assignArray (anArrPnt1, anArrPnt);
143     assignCollection (anArrPnt1, anArrPnt, "Assign collect to array");
144   } else {
145     TColgp_Array1OfPnt anArrPnt (1,100000), anArrPnt1 (1, 100000);
146     createArray (anArrPnt);
147     assignArray (anArrPnt1, anArrPnt);
148   }
149   printAllMeters (theDI);
150 }
151
152 static void checkSequence (Draw_Interpretor& theDI, 
153                            const Standard_Boolean isNewColl,
154                            const Standard_Boolean isIncr)
155 {
156   if (isNewColl) {
157     Handle(NCollection_BaseAllocator) anAlloc[2];
158     if (isIncr) {
159       anAlloc[0] = new NCollection_IncAllocator;
160       anAlloc[1] = new NCollection_IncAllocator;
161     }
162     MySequence aSeqPnt (anAlloc[0]), aSeqPnt1(anAlloc[1]);
163     createSequence (aSeqPnt);
164     assignSequence (aSeqPnt1, aSeqPnt);
165     assignCollection (aSeqPnt1, aSeqPnt, "Assign collect to sequence");
166   } else {
167     TColgp_SequenceOfPnt aSeqPnt, aSeqPnt1;
168     createSequence (aSeqPnt);
169     assignSequence (aSeqPnt1, aSeqPnt);
170   }
171   printAllMeters (theDI);
172 }
173
174 //=======================================================================
175 //function : QANColCheckArray1
176 //purpose  : 
177 //=======================================================================
178 static Standard_Integer QANColCheckArray1(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
179 {
180   if ( argc > 2) {
181     di << "Usage : " << argv[0] << " [-n]" << "\n";
182     return 1;
183   }
184
185   Standard_Boolean isNewColl = Standard_False;
186   if (argc > 1) {
187     if (strcmp (argv[1], "-n") == 0) isNewColl = Standard_True;
188   }
189   checkArray (di, isNewColl);
190   return 0;
191 }
192
193 //=======================================================================
194 //function : QANColCheckSequence
195 //purpose  : 
196 //=======================================================================
197 static Standard_Integer QANColCheckSequence(Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
198 {
199   if ( argc > 2) {
200     di << "Usage : " << argv[0] << " [-n]/[-ni]/[-in]" << "\n";
201     return 1;
202   }
203
204   Standard_Boolean isNewColl = Standard_False, isIncr = Standard_False;
205   if (argc > 1) {
206     if (strcmp (argv[1], "-n") == 0) isNewColl = Standard_True;
207     if (strcmp (argv[1], "-ni") == 0 || strcmp (argv[1], "-in") == 0)
208     {
209       isNewColl = Standard_True;
210       isIncr = Standard_True;
211     }
212   }
213   checkSequence (di, isNewColl, isIncr);
214   return 0;
215 }
216
217 void QANCollection::CommandsSimple(Draw_Interpretor& theCommands) {
218   const char *group = "QANCollection";
219
220   // from agvCollTest/src/AgvColEXE/TestEXE.cxx
221   theCommands.Add("QANColCheckArray1",   "QANColCheckArray1 [-n]",               __FILE__, QANColCheckArray1,   group);  
222   theCommands.Add("QANColCheckSequence", "QANColCheckSequence [-n]/[-ni]/[-in]", __FILE__, QANColCheckSequence, group);  
223
224   return;
225 }