Added the default lossless parameter to opj_set_default_encoder_parameters in openjpeg.c
[openjpeg.git] / v2 / libopenjpeg / profile.c
1 /*\r
2  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  * 1. Redistributions of source code must retain the above copyright\r
9  *    notice, this list of conditions and the following disclaimer.\r
10  * 2. Redistributions in binary form must reproduce the above copyright\r
11  *    notice, this list of conditions and the following disclaimer in the\r
12  *    documentation and/or other materials provided with the distribution.\r
13  *\r
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
24  * POSSIBILITY OF SUCH DAMAGE.\r
25  */\r
26 \r
27 /**\r
28  * Adapted from Herb Marselas\r
29 "Profiling, Data Analysis, Scalability, and Magic Numbers: Meeting the Minimum System Requirements for AGE OF EMPIRES 2: THE AGE OF KINGS"\r
30 Game Developer magazine\r
31 June, 2000 issue.\r
32 */\r
33 \r
34 #include "profile.h"\r
35 #include <string.h>\r
36 #include <stdio.h>\r
37 #include <time.h>\r
38 //==============================================================================\r
39 static OPJ_PROFILE_LIST group_list [PGROUP_LASTGROUP];\r
40 \r
41 //==============================================================================\r
42 static void GetTimeStamp(OPJ_UINT32 *pdwtime);\r
43 \r
44 //==============================================================================\r
45 #define SetMajorSection(entry, major) \\r
46         { group_list[ entry ].section = entry ; \\r
47           group_list[ entry ].sectionName = #major ; }\r
48 \r
49 //==============================================================================\r
50 void _ProfInit(void)\r
51 {\r
52    // clear everything out\r
53    memset(group_list, 0, sizeof(group_list));\r
54 \r
55    // set groups and parents for timing\r
56    SetMajorSection(PGROUP_DWT,PGROUP_DWT);\r
57    SetMajorSection(PGROUP_T1, PGROUP_T1);\r
58    SetMajorSection(PGROUP_T2, PGROUP_T2);\r
59 } // ProfInit\r
60 \r
61 //==============================================================================\r
62 void _ProfStart (OPJ_PROFILE_GROUP group)\r
63 {\r
64    // make sure this hasn't been incorrectly started twice\r
65    if (group_list[group].start)\r
66    {\r
67       return;\r
68    }\r
69 \r
70    // get the start time\r
71    GetTimeStamp(&(group_list[group].start));\r
72 \r
73 } // _ProfStart\r
74 \r
75 //==============================================================================\r
76 void _ProfStop(OPJ_PROFILE_GROUP group)\r
77 {\r
78    // make sure we called start first\r
79    if (!group_list[group].start)\r
80    {\r
81       return;\r
82    }\r
83 \r
84    // get ending time\r
85    GetTimeStamp(&(group_list[group].end));\r
86 \r
87    // calculate this latest elapsed interval\r
88    group_list[group].total_time += group_list[group].end - group_list[group].start;\r
89 \r
90    // reset starting time\r
91    group_list[group].start = 0;\r
92 \r
93    // incr the number of calls made\r
94    ++group_list[group].totalCalls;\r
95 \r
96 } // _ProfStop\r
97 \r
98 //==============================================================================\r
99 #define proftracef(id,totalTime) \\r
100         fprintf(p, #id "\t%u\t\t%6.6f\t\t%12.6f\t%2.2f%%\n",  \\r
101                 group_list[ id ].totalCalls, \\r
102                (OPJ_FLOAT64) group_list[ id ].total_time / CLOCKS_PER_SEC, \\r
103                ((OPJ_FLOAT64) group_list[ id ].total_time / (group_list[ id ].totalCalls ? group_list[ id ].totalCalls : 1)), \\r
104                            ((OPJ_FLOAT64) group_list[ id ].total_time / totalTime * 100))\r
105 \r
106 #define proftracep(id,totalTime) \\r
107         printf(#id "\t%u\t\t%6.6f\t\t%12.6f\t%2.2f%%\n",  \\r
108                 group_list[ id ].totalCalls, \\r
109                (OPJ_FLOAT64) group_list[ id ].total_time / CLOCKS_PER_SEC, \\r
110                                                          ((OPJ_FLOAT64) group_list[ id ].total_time  / (group_list[ id ].totalCalls ? group_list[ id ].totalCalls : 1)), \\r
111                            ((OPJ_FLOAT64) group_list[ id ].total_time / totalTime * 100))\r
112 \r
113 //==============================================================================\r
114 void _ProfSave(const OPJ_CHAR * pFileName)\r
115 {\r
116         FILE *p = fopen(pFileName, "wt");\r
117         OPJ_FLOAT64 totalTime = 0.;\r
118         OPJ_UINT32 i;\r
119 \r
120         if (!p)\r
121         {\r
122                 return;\r
123         }\r
124 \r
125         for \r
126                 (i=0;i<PGROUP_LASTGROUP;++i)\r
127         {\r
128                 totalTime += group_list[i].total_time;\r
129         }\r
130 \r
131         fputs("\n\nProfile Data:\n", p);\r
132         fputs("description\tnb calls\ttotal time (sec)\ttime per call\t%% of section\n", p);\r
133 \r
134         proftracef(PGROUP_DWT,totalTime);\r
135         proftracef(PGROUP_T1,totalTime);\r
136         proftracef(PGROUP_T2,totalTime);        \r
137 \r
138    fputs("=== end of profile list ===\n\n", p);\r
139 \r
140    fclose(p);\r
141 \r
142 } // _ProfSave\r
143 \r
144 //==============================================================================\r
145 void _ProfPrint(void)\r
146 {\r
147         OPJ_FLOAT64 totalTime = 0.;\r
148         OPJ_UINT32 i;\r
149 \r
150         for \r
151                 (i=0;i<PGROUP_LASTGROUP;++i)\r
152         {\r
153                 totalTime += group_list[i].total_time;\r
154         }\r
155 \r
156         printf("\n\nProfile Data:\n");\r
157         printf("description\tnb calls\ttotal time (sec)\ttime per call\t%% of section\n");\r
158 \r
159         proftracep(PGROUP_RATE, totalTime);\r
160         proftracep(PGROUP_DC_SHIFT, totalTime);\r
161         proftracep(PGROUP_MCT, totalTime);\r
162         proftracep(PGROUP_DWT, totalTime);\r
163         proftracep(PGROUP_T1, totalTime);\r
164         proftracep(PGROUP_T2, totalTime);\r
165 \r
166         printf("\nTotal time: %6.3f second(s)\n", totalTime / CLOCKS_PER_SEC);\r
167 \r
168   printf("=== end of profile list ===\n\n");\r
169 \r
170 } // _ProfPrint\r
171 \r
172 //==============================================================================\r
173 static void GetTimeStamp(unsigned *time)\r
174 {\r
175    *time = clock();\r
176 \r
177 } // GetTimeStamp\r