Imported Upstream version 58.2
[platform/upstream/icu.git] / source / test / perf / leperf / letrperf.cpp
1 /**************************************************************************
2 *
3 *   Copyright (C) 2016 and later: Unicode, Inc. and others.
4 *   License & terms of use: http://www.unicode.org/copyright.html#License
5 *
6 ***************************************************************************
7 ***************************************************************************
8 *
9 *   Copyright (C) 2013, International Business Machines
10 *   Corporation and others.  All Rights Reserved.
11 *
12 ************************************************************************/
13 /**
14  * Usage:
15  * build against a configured (but not built) ICU.
16  * example: cc -O2 test_LETableReference.cpp -I. -I/xsrl/II/include -I/xsrl/E/icu/source/tools/ctestfw
17  */
18 #include "unicode/utimer.h"
19 #include "LETableReference.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #define ITEM_COUNT 10000
24
25 long *items = 0;
26
27 struct OneObject {
28   long items[ITEM_COUNT];
29 };
30
31 struct Long {
32   long v;
33 };
34
35 struct CompObject {
36   Long items[ITEM_COUNT];
37 };
38
39
40 void time_null(void * /*ref*/) {
41   for(int i=0;i<ITEM_COUNT;i++) {
42     if(items[i]==2) {
43       return;
44     }
45   }
46   puts("error");
47   abort();
48 }
49
50 void time_obj(void * ref) {
51   OneObject &obj = *((OneObject*)ref);
52   for(int i=0;i<ITEM_COUNT;i++) {
53     if(obj.items[i]==2) {
54       return;
55     }
56   }
57   puts("error");
58   abort();
59 }
60 void time_obj2(void * ref) {
61   long *items2 = ((OneObject*)ref)->items;
62   for(int i=0;i<ITEM_COUNT;i++) {
63     if(items2[i]==2) {
64       return;
65     }
66   }
67   puts("error");
68   abort();
69 }
70
71 void time_letr1(void * ref) {
72   OneObject &obj = *((OneObject*)ref);
73   LETableReference data((const le_uint8*)ref, sizeof(OneObject));
74   LEErrorCode success = LE_NO_ERROR;
75
76   LEReferenceTo<OneObject> stuff(data, success);
77   if(LE_FAILURE(success)) {
78     puts("failure");
79     abort();
80   }
81   long *items2 = ((OneObject*)ref)->items;
82   for(int i=0;i<ITEM_COUNT;i++) {
83     if(items[i]==2) {
84       return;
85     }
86   }
87   puts("error");
88   abort();
89 }
90
91
92 void time_letr2(void * ref) {
93   OneObject &obj = *((OneObject*)ref);
94   LETableReference data((const le_uint8*)ref, sizeof(OneObject));
95   LEErrorCode success = LE_NO_ERROR;
96
97   long *items2 = ((OneObject*)ref)->items;
98   for(int i=0;i<ITEM_COUNT;i++) {
99     LEReferenceTo<OneObject> stuff(data, success);
100     if(LE_FAILURE(success)) {
101       puts("failure");
102       abort();
103     }
104     if(items[i]==2) {
105       return;
106     }
107   }
108   puts("error");
109   abort();
110 }
111
112 static void time_letr3(void * ref) {
113   LETableReference data((const le_uint8*)ref, sizeof(OneObject));
114   LEErrorCode success = LE_NO_ERROR;
115   LEReferenceTo<CompObject> comp(data, success);  
116   LEReferenceToArrayOf<Long> longs(comp, success, (size_t)0, ITEM_COUNT);
117   if(LE_FAILURE(success)) {
118     puts("failure");
119     abort();
120   }
121
122   for(int i=0;i<ITEM_COUNT;i++) {
123     const Long &item = longs.getObject(i, success);
124     if(LE_FAILURE(success)) {
125       puts("failure");
126       abort();
127     }
128     if(item.v==2) {
129       return;
130     }
131   }
132   puts("error");
133   abort();
134 }
135
136
137 int main() {
138   double runTime = 2.0;
139   printf("Test of LETableReference<> timing. %.1fs per run.\n", runTime);
140   items = new long[ITEM_COUNT];
141   OneObject *oo = new OneObject();
142   CompObject *oo2 = new CompObject();
143   for(int i=0;i<ITEM_COUNT-1;i++) {
144     items[i] = oo->items[i] = oo2->items[i].v = (i%1024)+3;
145   }
146   items[ITEM_COUNT-1] = oo->items[ITEM_COUNT-1] = oo2->items[ITEM_COUNT-1].v = 2; // last one
147
148   puts("will call once..");
149   time_letr3((void*)oo2);
150   puts("testing all..");
151  
152   int32_t loopCount;
153   double time_taken;
154
155 #define showTime(x,y)  printf("%s:\ttesting...\r",  #x);   fflush(stdout); \
156   time_taken = utimer_loopUntilDone(runTime, &loopCount, x, y); \
157   printf("%s:\t%.1fs\t#%d\t%.1f/s\n", #x, time_taken, loopCount, loopCount/(double)time_taken);
158
159   // clear out cache
160   {
161     double oldTime = runTime;
162     runTime = 0.25;
163     showTime(time_null, NULL); 
164     showTime(time_null, NULL); 
165     showTime(time_null, NULL); 
166     showTime(time_null, NULL); 
167     runTime = oldTime;
168   }
169   puts("-- ready to start --");
170
171
172   showTime(time_null, NULL); 
173   showTime(time_obj, (void*)oo);
174   showTime(time_obj2, (void*)oo);
175   showTime(time_letr1, (void*)oo2);
176   showTime(time_letr2, (void*)oo2);
177   showTime(time_letr3, (void*)oo2);
178   showTime(time_null, NULL);
179   
180   delete [] items;
181   delete oo;
182   delete oo2;
183 }