Skip to content

Instantly share code, notes, and snippets.

@hakomo
Created January 27, 2019 08:55
Show Gist options
  • Save hakomo/361c05bd076593e54430e3753775b844 to your computer and use it in GitHub Desktop.
Save hakomo/361c05bd076593e54430e3753775b844 to your computer and use it in GitHub Desktop.
// Topcoder Server の時間計測は rdtsc 以外壊れています
// 以下のコードで正しく時間計測ができます
// サブミットするときに #define LOCAL を消してください
#define LOCAL
#ifdef LOCAL
#include <chrono>
inline double getTime() {
using namespace std::chrono;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() / 1000.0;
}
#else
inline double getTime() {
unsigned long long a, d;
__asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
return (d << 32 | a) / 2.8e9;
}
#endif
#include <iostream>
using namespace std;
int main() {
double startTime = getTime();
while (getTime() - startTime < 1.5) {
}
cout << getTime() - startTime << " seconds" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment