Skip to content

Commit 0389e23

Browse files
committed
Show parsing time.
1 parent e85155b commit 0389e23

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

loader_example.cc

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,89 @@
1111
#include <sstream>
1212
#include <fstream>
1313

14+
#ifdef _WIN32
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
#include <windows.h>
19+
#include <mmsystem.h>
20+
#ifdef __cplusplus
21+
}
22+
#endif
23+
#pragma comment(lib, "winmm.lib")
24+
#else
25+
#if defined(__unix__) || defined(__APPLE__)
26+
#include <sys/time.h>
27+
#else
28+
#include <ctime>
29+
#endif
30+
#endif
31+
32+
class timerutil {
33+
public:
34+
#ifdef _WIN32
35+
typedef DWORD time_t;
36+
37+
timerutil() { ::timeBeginPeriod(1); }
38+
~timerutil() { ::timeEndPeriod(1); }
39+
40+
void start() { t_[0] = ::timeGetTime(); }
41+
void end() { t_[1] = ::timeGetTime(); }
42+
43+
time_t sec() { return (time_t)((t_[1] - t_[0]) / 1000); }
44+
time_t msec() { return (time_t)((t_[1] - t_[0])); }
45+
time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000); }
46+
time_t current() { return ::timeGetTime(); }
47+
48+
#else
49+
#if defined(__unix__) || defined(__APPLE__)
50+
typedef unsigned long int time_t;
51+
52+
void start() { gettimeofday(tv + 0, &tz); }
53+
void end() { gettimeofday(tv + 1, &tz); }
54+
55+
time_t sec() { return static_cast<time_t>(tv[1].tv_sec - tv[0].tv_sec); }
56+
time_t msec() {
57+
return this->sec() * 1000 +
58+
static_cast<time_t>((tv[1].tv_usec - tv[0].tv_usec) / 1000);
59+
}
60+
time_t usec() {
61+
return this->sec() * 1000000 + static_cast<time_t>(tv[1].tv_usec - tv[0].tv_usec);
62+
}
63+
time_t current() {
64+
struct timeval t;
65+
gettimeofday(&t, NULL);
66+
return static_cast<time_t>(t.tv_sec * 1000 + t.tv_usec);
67+
}
68+
69+
#else // C timer
70+
// using namespace std;
71+
typedef clock_t time_t;
72+
73+
void start() { t_[0] = clock(); }
74+
void end() { t_[1] = clock(); }
75+
76+
time_t sec() { return (time_t)((t_[1] - t_[0]) / CLOCKS_PER_SEC); }
77+
time_t msec() { return (time_t)((t_[1] - t_[0]) * 1000 / CLOCKS_PER_SEC); }
78+
time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000000 / CLOCKS_PER_SEC); }
79+
time_t current() { return (time_t)clock(); }
80+
81+
#endif
82+
#endif
83+
84+
private:
85+
#ifdef _WIN32
86+
DWORD t_[2];
87+
#else
88+
#if defined(__unix__) || defined(__APPLE__)
89+
struct timeval tv[2];
90+
struct timezone tz;
91+
#else
92+
time_t t_[2];
93+
#endif
94+
#endif
95+
};
96+
1497
static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials)
1598
{
1699
std::cout << "# of vertices : " << (attrib.vertices.size() / 3) << std::endl;
@@ -55,7 +138,7 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
55138
for (size_t f = 0; f < shapes[i].mesh.num_face_vertices.size(); f++) {
56139
size_t fnum = shapes[i].mesh.num_face_vertices[f];
57140

58-
printf(" face[%ld].fnum = %d\n", static_cast<long>(f), static_cast<unsigned long>(fnum));
141+
printf(" face[%ld].fnum = %ld\n", static_cast<long>(f), static_cast<unsigned long>(fnum));
59142

60143
// For each vertex in the face
61144
for (size_t v = 0; v < fnum; v++) {
@@ -147,8 +230,12 @@ TestLoadObj(
147230
std::vector<tinyobj::shape_t> shapes;
148231
std::vector<tinyobj::material_t> materials;
149232

233+
timerutil t;
234+
t.start();
150235
std::string err;
151236
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, basepath, triangulate);
237+
t.end();
238+
printf("Parsing time: %lu [msecs]\n", t.msec());
152239

153240
if (!err.empty()) {
154241
std::cerr << err << std::endl;

0 commit comments

Comments
 (0)