88#include < sstream>
99#include < fstream>
1010
11+ #ifdef _WIN32
12+ #ifdef __cplusplus
13+ extern " C" {
14+ #endif
15+ #include < windows.h>
16+ #include < mmsystem.h>
17+ #ifdef __cplusplus
18+ }
19+ #endif
20+ #pragma comment(lib, "winmm.lib")
21+ #else
22+ #if defined(__unix__) || defined(__APPLE__)
23+ #include < sys/time.h>
24+ #else
25+ #include < ctime>
26+ #endif
27+ #endif
28+
29+ // not thread-safe
30+ class timerutil {
31+ public:
32+ #ifdef _WIN32
33+ typedef DWORD time_t ;
34+
35+ timerutil () { ::timeBeginPeriod (1 ); }
36+ ~timerutil () { ::timeEndPeriod (1 ); }
37+
38+ void start () { t_[0 ] = ::timeGetTime (); }
39+ void end () { t_[1 ] = ::timeGetTime (); }
40+
41+ time_t sec () { return (time_t )((t_[1 ] - t_[0 ]) / 1000 ); }
42+ time_t msec () { return (time_t )((t_[1 ] - t_[0 ])); }
43+ time_t usec () { return (time_t )((t_[1 ] - t_[0 ]) * 1000 ); }
44+ time_t current () { return ::timeGetTime (); }
45+
46+ #else
47+ #if defined(__unix__) || defined(__APPLE__)
48+ typedef unsigned long int time_t ;
49+
50+ void start () { gettimeofday (tv + 0 , &tz); }
51+ void end () { gettimeofday (tv + 1 , &tz); }
52+
53+ time_t sec () { return (time_t )(tv[1 ].tv_sec - tv[0 ].tv_sec ); }
54+ time_t msec () {
55+ return this ->sec () * 1000 +
56+ (time_t )((tv[1 ].tv_usec - tv[0 ].tv_usec ) / 1000 );
57+ }
58+ time_t usec () {
59+ return this ->sec () * 1000000 + (time_t )(tv[1 ].tv_usec - tv[0 ].tv_usec );
60+ }
61+ time_t current () {
62+ struct timeval t;
63+ gettimeofday (&t, NULL );
64+ return (time_t )(t.tv_sec * 1000 + t.tv_usec );
65+ }
66+
67+ #else // C timer
68+ // using namespace std;
69+ typedef clock_t time_t ;
70+
71+ void start () { t_[0 ] = clock (); }
72+ void end () { t_[1 ] = clock (); }
73+
74+ time_t sec () { return (time_t )((t_[1 ] - t_[0 ]) / CLOCKS_PER_SEC); }
75+ time_t msec () { return (time_t )((t_[1 ] - t_[0 ]) * 1000 / CLOCKS_PER_SEC); }
76+ time_t usec () { return (time_t )((t_[1 ] - t_[0 ]) * 1000000 / CLOCKS_PER_SEC); }
77+ time_t current () { return (time_t )clock (); }
78+
79+ #endif
80+ #endif
81+
82+ private:
83+ #ifdef _WIN32
84+ DWORD t_[2 ];
85+ #else
86+ #if defined(__unix__) || defined(__APPLE__)
87+ struct timeval tv[2 ];
88+ struct timezone tz;
89+ #else
90+ time_t t_[2 ];
91+ #endif
92+ #endif
93+ };
94+
1195static void PrintInfo (const std::vector<tinyobj::shape_t >& shapes, const std::vector<tinyobj::material_t >& materials, bool triangulate = true )
1296{
1397 std::cout << " # of shapes : " << shapes.size () << std::endl;
@@ -131,8 +215,11 @@ TestLoadObj(
131215 std::vector<tinyobj::shape_t > shapes;
132216 std::vector<tinyobj::material_t > materials;
133217
218+ timerutil t;
219+ t.start ();
134220 std::string err;
135221 bool ret = tinyobj::LoadObj (shapes, materials, err, filename, basepath, flags);
222+ t.end ();
136223
137224 if (!err.empty ()) {
138225 std::cerr << err << std::endl;
@@ -143,6 +230,8 @@ TestLoadObj(
143230 return false ;
144231 }
145232
233+ printf (" Parse time: %lu [msecs]\n " , t.msec ());
234+
146235 bool triangulate ( ( flags & tinyobj::triangulation ) == tinyobj::triangulation );
147236 PrintInfo (shapes, materials, triangulate );
148237
0 commit comments