diff --git a/loader_example.cc b/loader_example.cc index 0aec2d1..340b071 100644 --- a/loader_example.cc +++ b/loader_example.cc @@ -11,6 +11,89 @@ #include #include +#ifdef _WIN32 +#ifdef __cplusplus +extern "C" { +#endif +#include +#include +#ifdef __cplusplus +} +#endif +#pragma comment(lib, "winmm.lib") +#else +#if defined(__unix__) || defined(__APPLE__) +#include +#else +#include +#endif +#endif + +class timerutil { +public: +#ifdef _WIN32 + typedef DWORD time_t; + + timerutil() { ::timeBeginPeriod(1); } + ~timerutil() { ::timeEndPeriod(1); } + + void start() { t_[0] = ::timeGetTime(); } + void end() { t_[1] = ::timeGetTime(); } + + time_t sec() { return (time_t)((t_[1] - t_[0]) / 1000); } + time_t msec() { return (time_t)((t_[1] - t_[0])); } + time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000); } + time_t current() { return ::timeGetTime(); } + +#else +#if defined(__unix__) || defined(__APPLE__) + typedef unsigned long int time_t; + + void start() { gettimeofday(tv + 0, &tz); } + void end() { gettimeofday(tv + 1, &tz); } + + time_t sec() { return static_cast(tv[1].tv_sec - tv[0].tv_sec); } + time_t msec() { + return this->sec() * 1000 + + static_cast((tv[1].tv_usec - tv[0].tv_usec) / 1000); + } + time_t usec() { + return this->sec() * 1000000 + static_cast(tv[1].tv_usec - tv[0].tv_usec); + } + time_t current() { + struct timeval t; + gettimeofday(&t, NULL); + return static_cast(t.tv_sec * 1000 + t.tv_usec); + } + +#else // C timer + // using namespace std; + typedef clock_t time_t; + + void start() { t_[0] = clock(); } + void end() { t_[1] = clock(); } + + time_t sec() { return (time_t)((t_[1] - t_[0]) / CLOCKS_PER_SEC); } + time_t msec() { return (time_t)((t_[1] - t_[0]) * 1000 / CLOCKS_PER_SEC); } + time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000000 / CLOCKS_PER_SEC); } + time_t current() { return (time_t)clock(); } + +#endif +#endif + +private: +#ifdef _WIN32 + DWORD t_[2]; +#else +#if defined(__unix__) || defined(__APPLE__) + struct timeval tv[2]; + struct timezone tz; +#else + time_t t_[2]; +#endif +#endif +}; + static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector& shapes, const std::vector& materials) { 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(f), static_cast(fnum)); + printf(" face[%ld].fnum = %ld\n", static_cast(f), static_cast(fnum)); // For each vertex in the face for (size_t v = 0; v < fnum; v++) { @@ -147,8 +230,12 @@ TestLoadObj( std::vector shapes; std::vector materials; + timerutil t; + t.start(); std::string err; bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, basepath, triangulate); + t.end(); + printf("Parsing time: %lu [msecs]\n", t.msec()); if (!err.empty()) { std::cerr << err << std::endl;