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