]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/howExpensiveIs/howExpensiveIs.cpp
2 **********************************************************************
3 * Copyright (c) 2011,International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
9 #include "unicode/utimer.h"
15 UErrorCode setupStatus
= U_ZERO_ERROR
;
17 int main ( int argc
, const char * argv
[]){
19 fprintf ( stderr
, " %s : warning: U_DEBUG is on. \n " , argv
[ 0 ]);
24 double s
= uprv_getSieveTime (& m
);
25 fprintf ( stderr
, "** Standard sieve time: %.9fs +/- %.9fs ( %d iterations) \n " , s
, m
, ( int ) U_LOTS_OF_TIMES
);
30 out
= fopen ( argv
[ 1 ], "w" );
32 fprintf ( stderr
, "Err: can't open %s for writing. \n " , argv
[ 1 ]);
35 fprintf ( out
, "<?xml version= \" 1.0 \" encoding= \" UTF-8 \" ?> \n " );
36 fprintf ( out
, "<tests icu= \" %s \" > \n " , U_ICU_VERSION
);
37 fprintf ( out
, "<!-- %s --> \n " , U_COPYRIGHT_STRING
);
39 fprintf ( stderr
, "Err: usage: %s [ output-file.xml ] \n " , argv
[ 0 ]);
47 udbg_writeIcuInfo ( out
);
48 fprintf ( out
, "</tests> \n " );
52 if ( U_FAILURE ( setupStatus
)) {
53 fprintf ( stderr
, "Error in tests: %s \n " , u_errorName ( setupStatus
));
60 class HowExpensiveTest
{
62 virtual ~ HowExpensiveTest (){}
64 HowExpensiveTest ( const char * name
, const char * file
, int32_t line
) : fName ( name
), fFile ( file
), fLine ( line
) {}
67 * @return number of iterations
69 virtual int32_t run () = 0 ;
70 virtual void warmup () { run (); }
72 virtual int32_t runTest ( double * subTime
) {
77 * subTime
= utimer_getDeltaSeconds (& a
,& b
);
81 virtual int32_t runTests ( double * subTime
, double * marginOfError
) {
82 warmup (); /* warmup */
84 double times
[ ITERATIONS
];
85 int subIterations
= 0 ;
86 for ( int i
= 0 ; i
< ITERATIONS
; i
++) {
87 subIterations
= runTest (& times
[ i
]);
89 fprintf ( stderr
, "trial: %d / %d = %.9fs \n " , i
, ITERATIONS
, times
[ i
]);
93 * subTime
= uprv_getMeanTime ( times
, ITERATIONS
, marginOfError
);
103 void runTestOn ( HowExpensiveTest
& t
) {
104 fprintf ( stderr
, " %s : %d : Running: %s \n " , t
. fFile
, t
. fLine
, t
. fName
);
105 double sieveTime
= uprv_getSieveTime ( NULL
);
111 int32_t iter
= t
. runTests (& st
,& me
);
115 double stn
= st
/ sieveTime
;
117 printf ( " %s \t %.9f \t %.9f +/- %.9f, @ %d iter \n " , t
. fName
, stn
, st
, me
, iter
);
120 fprintf ( out
, " <test name= \" %s \" standardizedTime= \" %f \" realDuration= \" %f \" marginOfError= \" %f \" iterations= \" %d \" /> \n " ,
121 t
. fName
, stn
, st
, me
, iter
);
126 /* ------------------- test code here --------------------- */
128 class SieveTest
: public HowExpensiveTest
{
130 virtual ~ SieveTest (){}
131 SieveTest (): HowExpensiveTest ( "SieveTest" , __FILE__
, __LINE__
){}
132 virtual int32_t run (){ return 0 ;} // dummy
133 int32_t runTest ( double * subTime
) {
134 * subTime
= uprv_getSieveTime ( NULL
);
135 return U_LOTS_OF_TIMES
;
137 virtual int32_t runTests ( double * subTime
, double * marginOfError
) {
138 * subTime
= uprv_getSieveTime ( marginOfError
);
139 return U_LOTS_OF_TIMES
;
144 /* ------- NumParseTest ------------- */
145 #include "unicode/unum.h"
146 /* open and close tests */
147 #define OCName(svc,ub,testn,suffix,n) testn ## svc ## ub ## suffix ## n
148 #define OCStr(svc,ub,suffix,n) "Test_" # svc # ub # suffix # n
149 #define OCRun(svc,ub,suffix) svc ## ub ## suffix
150 // TODO: run away screaming
151 #define OpenCloseTest(n, svc,suffix,c,a,d) class OCName(svc,_,Test_,suffix,n) : public HowExpensiveTest { public: OCName(svc,_,Test_,suffix,n)():HowExpensiveTest(OCStr(svc,_,suffix,n),__FILE__,__LINE__) c int32_t run() { int32_t i; for(i=0;i<U_LOTS_OF_TIMES;i++){ OCRun(svc,_,close) ( OCRun(svc,_,suffix) a ); } return i; } void warmup() { OCRun(svc,_,close) ( OCRun(svc,_,suffix) a); } virtual ~ OCName(svc,_,Test_,suffix,n) () d };
152 #define QuickTest(n,c,r,d) class n : public HowExpensiveTest { public: n():HowExpensiveTest(#n,__FILE__,__LINE__) c int32_t run() r virtual ~n () d };
154 // TODO: move, scope.
155 static UChar pattern
[] = { 0x23 }; // '#'
157 UNumberFormat
* NumParseTest_fmt
;
160 QuickTest ( NumParseTest
,{ static UChar pattern
[] = { 0x23 }; NumParseTest_fmt
= unum_open ( UNUM_PATTERN_DECIMAL
, pattern
, 1 , "en_US" , 0 , & setupStatus
); },{ int32_t i
; static UChar str
[] = { 0x31 }; double val
; for ( i
= 0 ; i
< U_LOTS_OF_TIMES
; i
++) { val
= unum_parse ( NumParseTest_fmt
, str
, 1 , NULL
,& setupStatus
); } return i
; },{ unum_close ( NumParseTest_fmt
);})
163 QuickTest ( NullTest
,{},{ int j
= U_LOTS_OF_TIMES
; while (-- j
); return U_LOTS_OF_TIMES
;},{})
164 OpenCloseTest ( pattern
, unum
, open
,{},( UNUM_PATTERN_DECIMAL
, pattern
, 1 , "en_US" , 0 ,& setupStatus
),{})
165 OpenCloseTest ( default , unum
, open
,{},( UNUM_DEFAULT
, NULL
,- 1 , "en_US" , 0 ,& setupStatus
),{})
166 #include "unicode/ucnv.h"
167 OpenCloseTest ( gb18030
, ucnv
, open
,{},( "gb18030" ,& setupStatus
),{})
168 #include "unicode/ures.h"
169 OpenCloseTest ( root
, ures
, open
,{},( NULL
, "root" ,& setupStatus
),{})
185 Test_unum_opendefault t
;
189 Test_ucnv_opengb18030 t
;
193 Test_unum_openpattern t
;
197 Test_ures_openroot t
;