Created
May 5, 2015 02:14
-
-
Save theidexisted/25aa7456cddcbb1a53f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example program | |
#include <iostream> | |
#include <string> | |
#include <cstdio> | |
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
#include <memory> | |
#include <functional> | |
#include <iterator> | |
#include <fstream> | |
#ifdef _WIN32 | |
#include <windows.h> | |
class Timer { | |
public: | |
Timer() : start_(), end_() { | |
} | |
void Start() { | |
QueryPerformanceCounter(&start_); | |
} | |
void Stop() { | |
QueryPerformanceCounter(&end_); | |
} | |
double GetElapsedMilliseconds() { | |
LARGE_INTEGER freq; | |
QueryPerformanceFrequency(&freq); | |
return (end_.QuadPart - start_.QuadPart) * 1000.0 / freq.QuadPart; | |
} | |
private: | |
LARGE_INTEGER start_; | |
LARGE_INTEGER end_; | |
}; | |
#elif defined unix | |
#include <sys/time.h> | |
class Timer { | |
public: | |
Timer() : start_(), end_() { | |
} | |
void Start() { | |
gettimeofday(&start_, NULL); | |
} | |
void Stop() { | |
gettimeofday(&end_, NULL); | |
} | |
double GetElapsedMilliseconds() { | |
return (end_.tv_sec - start_.tv_sec) * 1000.0 | |
+ (end_.tv_usec - start_.tv_usec) / 1000.0; | |
} | |
private: | |
struct timeval start_; | |
struct timeval end_; | |
}; | |
#else | |
#error platform not detected | |
#endif // __WIN32__ | |
Timer gtimer; | |
std::ofstream fs("test.txt"); | |
std::ofstream fs1("test1.txt"); | |
bool g_LoggingEnabled = true; | |
#define LOG(msg) \ | |
if (g_LoggingEnabled) \ | |
(Log(__FILE__, __LINE__, LogData<None>() << msg)) | |
template <typename List> | |
struct LogData { | |
typedef List type; | |
List list; | |
}; | |
struct None{}; | |
template <typename T> | |
struct DT; | |
template <typename Begin, typename Value> | |
LogData<std::pair<Begin&&, Value&&>> | |
operator<<(LogData<Begin>&& begin, Value&& v) noexcept { | |
return {{ std::forward<Begin>(begin.list), std::forward<Value>(v) }}; | |
} | |
template <typename TLogData> | |
__attribute__((__noinline__)) | |
void Log(const char* file, int line, TLogData&& data) noexcept { | |
fs << file << "(" << line << "):"; | |
Log_Recursive(fs, | |
std::forward<typename TLogData::type>(data.list)); | |
fs << std::endl; | |
} | |
template <typename TLogDataPair> | |
void Log_Recursive(std::ostream& os, TLogDataPair&& data) noexcept { | |
Log_Recursive(os, | |
std::forward<typename TLogDataPair::first_type>(data.first)); | |
os << std::forward<typename TLogDataPair::second_type>(data.second); | |
} | |
inline void Log_Recursive(std::ostream& os, None) noexcept { | |
} | |
/* | |
typedef std::ostream& (*pfnManipulator) (std::ostream&); | |
template <typename Begin> | |
LogData<std::pair<Begin&&, pfnManipulator>> | |
operator <<(LogData<Begin>&& begin, pfnManipulator pfn) noexcept { | |
return {{ std::forward<Begin>(begin.list), pfn }}; | |
} | |
*/ | |
template <typename Begin, size_t n> | |
LogData<std::pair<Begin&&, const char*>> | |
operator <<(LogData<Begin>&& begin, const char (&sz) [n]) noexcept { | |
return {{ std::forward<Begin>(begin.list), sz }}; | |
} | |
#define SimpleLog(msg) \ | |
fs1 << __FILE__ << __LINE__ << msg; | |
int main() { | |
const int kMax = 1000; | |
gtimer.Start(); | |
for (int i = 0; i < kMax; ++i) { | |
std::string file = "file.in"; | |
int error = 0; | |
LOG("Read failed" << file << "(" << error << ")"); | |
} | |
gtimer.Stop(); | |
std::cout << gtimer.GetElapsedMilliseconds() << std::endl; | |
gtimer.Start(); | |
for (int i = 0; i < kMax; ++i) { | |
std::string file = "file.in"; | |
int error = 0; | |
SimpleLog("Read failed" << file << "(" << error << ")"); | |
} | |
gtimer.Stop(); | |
std::cout << gtimer.GetElapsedMilliseconds() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment