Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. // Krzysztof Małysa
  2. #include <bits/stdc++.h>
  3. #include <unistd.h>
  4.  
  5. using namespace std;
  6.  
  7. #define FOR(i,a,n) for (int i = (a), __n ## i = n; i <= __n ## i; ++i)
  8. #define REP(i,n) FOR(i,0,n)
  9. #define FORD(i,a,n) for (int i = (a), __n ## i = n; i >= __n ## i; --i)
  10. #define ALL(x) (x).begin(), (x).end()
  11. #define SZ(x) (int(x.size()))
  12. #define eprintf(...) fprintf(stderr, __VA_ARGS__)
  13. #define ST first
  14. #define ND second
  15. #define MP make_pair
  16. #define PB push_back
  17. #define EB emplace_back
  18. #define O(...) ostream& operator <<(ostream& os, const __VA_ARGS__& x)
  19. #define OO(...) O(__VA_ARGS__) { return __out(os, ALL(x)); }
  20. #define T template
  21. #define CL class
  22.  
  23. typedef unsigned uint;
  24. typedef long long LL;
  25. typedef unsigned long long ULL;
  26. typedef vector<int> VI;
  27. typedef vector<VI> VVI;
  28. typedef pair<int, int> PII;
  29. typedef vector<PII> VPII;
  30. typedef pair<LL, LL> PLL;
  31.  
  32. T<CL A> inline A abs(const A& a) { return a < A() ? -a : a; }
  33. T<CL A, CL B> inline void mini(A& a, const B& b) { if (b < a) a = b; }
  34. T<CL A, CL B> inline void maxi(A& a, const B& b) { if (b > a) a = b; }
  35.  
  36. T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s = ", ");
  37. T<CL A, CL B> O(pair<A, B>);
  38.  
  39. T<CL A> OO(vector<A>)
  40. T<CL A> OO(deque<A>)
  41. T<CL A> OO(list<A>)
  42. T<CL A, CL B> OO(set<A, B>)
  43. T<CL A, CL B> OO(multiset<A, B>)
  44. T<CL A, CL B, CL C> OO(map<A, B, C>)
  45. T<CL A, CL B, CL C> OO(multimap<A, B, C>)
  46.  
  47. T<CL Iter> ostream& __out(ostream& os, Iter a, Iter b, const string& s) {
  48. os << "{";
  49. if (a != b) {
  50. os << *a;
  51. while (++a != b)
  52. os << s << *a;
  53. }
  54. return os << "}";
  55. }
  56.  
  57. T<CL Iter> ostream& __dump(ostream& os, Iter a, Iter b) {
  58. os << "{\n";
  59. Iter beg = a;
  60. while (a != b) {
  61. os << " " << a - beg << ": " << *a << "\n";
  62. ++a;
  63. }
  64. return os << "}";
  65. }
  66.  
  67. T<CL A, CL B> O(pair<A, B>) { return os << "(" << x.ST << ", " << x.ND << ")"; }
  68.  
  69. #undef LIKELY
  70. #undef UNLIKELY
  71.  
  72. #if defined(__GNUC__) && __GNUC__ >= 4
  73. # define LIKELY(x) (__builtin_expect((x), 1))
  74. # define UNLIKELY(x) (__builtin_expect((x), 0))
  75. #else
  76. # define LIKELY(x) (x)
  77. # define UNLIKELY(x) (x)
  78. #endif
  79.  
  80. CL Input {
  81. static const int BUFF_SIZE = 1 << 16;
  82. const int fd;
  83. unsigned char buff[BUFF_SIZE], *pos, *end;
  84.  
  85. void grabBuffer() { end = (pos = buff) + read(fd, buff, BUFF_SIZE); }
  86.  
  87. public:
  88. explicit Input(int x) : fd(x), pos(buff), end(buff) {}
  89.  
  90. int peek() {
  91. if (UNLIKELY(pos == end))
  92. grabBuffer();
  93. return LIKELY(pos != end) ? *pos : -1;
  94. }
  95.  
  96. int getChar() {
  97. if (UNLIKELY(pos == end))
  98. grabBuffer();
  99. return LIKELY(pos != end) ? *pos++ : -1;
  100. }
  101.  
  102. void skipWhiteSpaces() {
  103. while (isspace(peek()))
  104. ++pos;
  105. }
  106.  
  107. Input& operator>>(char *s) {
  108. skipWhiteSpaces();
  109. while (!isspace(peek()))
  110. *s++ = *pos++;
  111. *s = '\0';
  112. return *this;
  113. }
  114.  
  115. T<CL A>
  116. Input& operator>>(A& a);
  117. } fin(0);
  118.  
  119. T<> Input& Input::operator>> <char>(char& x) {
  120. skipWhiteSpaces();
  121. x = *pos++;
  122. return *this;
  123. }
  124.  
  125. T<> Input& Input::operator>> <uint>(uint& x) {
  126. skipWhiteSpaces();
  127. x = 0;
  128. while (isdigit(peek()))
  129. x = x * 10 + *pos++ - '0';
  130. return *this;
  131. }
  132.  
  133. T<> Input& Input::operator>> <int>(int& x) {
  134. skipWhiteSpaces();
  135. if (peek() != '-')
  136. return operator>> <uint>((uint&)x);
  137. ++pos; operator>> <uint>((uint&)x); x = -x;
  138. return *this;
  139. }
  140.  
  141. T<> Input& Input::operator>> <unsigned short>(unsigned short& x) {
  142. skipWhiteSpaces();
  143. x = 0;
  144. while (isdigit(peek()))
  145. x = x * 10 + *pos++ - '0';
  146. return *this;
  147. }
  148.  
  149. T<> Input& Input::operator>> <short>(short& x) {
  150. skipWhiteSpaces();
  151. if (peek() != '-')
  152. return operator>> <unsigned short>((unsigned short&)x);
  153. ++pos; operator>> <unsigned short>((unsigned short&)x); x = -x;
  154. return *this;
  155. }
  156.  
  157. T<> Input& Input::operator>> <ULL>(ULL& x) {
  158. skipWhiteSpaces();
  159. x = 0;
  160. while (isdigit(peek()))
  161. x = x * 10 + *pos++ - '0';
  162. return *this;
  163. }
  164.  
  165. T<> Input& Input::operator>> <LL>(LL& x) {
  166. skipWhiteSpaces();
  167. if (peek() != '-')
  168. return operator>> <ULL>((ULL&)x);
  169. ++pos; operator>> <ULL>((ULL&)x); x = -x;
  170. return *this;
  171. }
  172.  
  173. T<> Input& Input::operator>> <string>(string& x) {
  174. skipWhiteSpaces();
  175. x.clear();
  176. while (!isspace(peek()))
  177. x += *pos++;
  178. return *this;
  179. }
  180.  
  181. inline int ceil2(int x) { return 1 << (sizeof(x) * 8 -__builtin_clz(x - 1)); }
  182.  
  183. #undef T
  184. #undef CL
  185. #ifdef DEBUG
  186. # define D(...) __VA_ARGS__
  187. #else
  188. # define D(...)
  189. #endif
  190.  
  191. #define E(...) D(eprintf(__VA_ARGS__))
  192. #define OUT(a,b) D(cerr << #a ": ", __out(cerr, a, b), E("\n"))
  193. #define DUMP(x) D(cerr << #x ": ", __dump(cerr, ALL(x)), E("\n"))
  194. #define LOG(x) D(cerr << #x ": " << (x))
  195. #define LOG2(x, y) D(cerr << x << ": " << (y))
  196. #define LOGN(x) D(LOG(x) << endl)
  197. #define LOGN2(x, y) D(LOG2(x, y) << endl)
  198. /// End of templates
  199.  
  200. template<class A, class B>
  201. inline int fastMod(A x, B mod) { return x < mod ? x : x % mod; }
  202.  
  203. class Stopwatch {
  204. std::chrono::steady_clock::time_point begin;
  205.  
  206. public:
  207. Stopwatch() noexcept { restart(); }
  208.  
  209. void restart() noexcept { begin = std::chrono::steady_clock::now(); }
  210.  
  211. long long microtime() const noexcept {
  212. using namespace std::chrono;
  213. return duration_cast<microseconds>(steady_clock::now() - begin).count();
  214. }
  215.  
  216. double time() const noexcept {
  217. using namespace std::chrono;
  218. return duration<double>(steady_clock::now() - begin).count();
  219. }
  220. };
  221.  
  222. #include <sys/stat.h>
  223. #include <sys/wait.h>
  224.  
  225. int getUnlinkedTmpFile(int flags = 0) noexcept {
  226. int fd;
  227. #ifdef O_TMPFILE
  228. fd = open("/tmp", O_TMPFILE | O_RDWR | flags, S_0600);
  229. if (fd != -1)
  230. return fd;
  231.  
  232. if (errno != EINVAL)
  233. return -1;
  234. #endif
  235.  
  236. char name[] = "/tmp/tmp_unlinked_file.XXXXXX";
  237. umask(077); // Only owner can access this temporary file
  238. fd = mkostemp(name, flags);
  239. if (fd == -1)
  240. return -1;
  241.  
  242. (void)unlink(name);
  243. return fd;
  244. }
  245.  
  246. inline int sclose(int fd) noexcept {
  247. while (close(fd) == -1)
  248. if (errno != EINTR)
  249. return -1;
  250.  
  251. return 0;
  252. }
  253.  
  254. mt19937 _r_gen(chrono::system_clock::now().time_since_epoch().count());
  255. int getRandom(int a, int b) {
  256. return uniform_int_distribution<int>(a, b)(_r_gen);
  257. }
  258.  
  259. #define BENCHMARK(...)
  260.  
  261. void benchCinInt(int n) {
  262. int x = 0;
  263. REP (i, n - 1)
  264. cin >> x;
  265. cout << x << endl;
  266. }
  267.  
  268. void benchFinInt(int n) {
  269. int x = 0;
  270. REP (i, n - 1)
  271. fin >> x;
  272. cout << x << endl;
  273. }
  274.  
  275. void benchCinString(int) {
  276. string s;
  277. cin >> s;
  278. cout << s.size() << endl;
  279. }
  280.  
  281. void benchFinString(int) {
  282. string s;
  283. fin >> s;
  284. cout << s.size() << endl;
  285. }
  286.  
  287. void benchCinCstring(int n) {
  288. unique_ptr<char[]> s(new char[n + 1]);
  289. cin >> s.get();
  290. cout << strlen(s.get()) << endl;
  291. }
  292.  
  293. void benchFinCstring(int n) {
  294. unique_ptr<char[]> s(new char[n + 1]);
  295. fin >> s.get();
  296. cout << strlen(s.get()) << endl;
  297. }
  298.  
  299. void benchScanfInt(int n) {
  300. int x = 0;
  301. REP (i, n - 1)
  302. scanf("%i", &x);
  303. cout << x << endl;
  304. }
  305.  
  306. void benchScanfCstring(int n) {
  307. unique_ptr<char[]> s(new char[n + 1]);
  308. scanf("%s", s.get());
  309. cout << strlen(s.get()) << endl;
  310. }
  311.  
  312. void benchCoutInt(int n) {
  313. VI t(n);
  314. for (int& i : t)
  315. i = _r_gen();
  316. for (int i : t)
  317. cout << i << '\n';
  318. cout << flush;
  319. }
  320.  
  321. void benchCoutString(int n) {
  322. string s(n, ' ');
  323. for (char& c : s)
  324. c = getRandom('A', 'z');
  325. cout << s << '\n' << flush;
  326. }
  327.  
  328. void benchCoutCstring(int n) {
  329. unique_ptr<char[]> s(new char[n + 1]);
  330. s[n] = '\0';
  331. REP (i, n - 1)
  332. s[i] = getRandom('A', 'z');
  333. cout << s.get() << '\n' << flush;
  334. }
  335.  
  336. void benchPrintfInt(int n) {
  337. VI t(n);
  338. for (int& i : t)
  339. i = _r_gen();
  340. for (int i : t)
  341. printf("%i\n", i);
  342. }
  343.  
  344. void benchPrintfCstring(int n) {
  345. unique_ptr<char[]> s(new char[n + 1]);
  346. s[n] = '\0';
  347. REP (i, n - 1)
  348. s[i] = getRandom('A', 'z');
  349. printf("%s\n", s.get());
  350. }
  351.  
  352. template<class Gen, class Func>
  353. void run(Gen gen, Func func, int n, const string& bench_name) {
  354. int fd = getUnlinkedTmpFile();
  355. assert(fd != -1);
  356. FILE *f = fdopen(fd, "rw");
  357. assert(f);
  358.  
  359. gen(f, n);
  360. lseek(fd, 0, SEEK_SET);
  361.  
  362. pid_t pid = fork();
  363. assert(pid != -1);
  364. if (pid == 0) {
  365. dup2(STDOUT_FILENO, STDERR_FILENO);
  366. dup2(fd, STDIN_FILENO);
  367. freopen("/tmp/bench-test.txt", "w", stdout);
  368. Stopwatch st;
  369. func(n);
  370. cerr << bench_name << ":\t" << setprecision(6) << fixed << st.time()
  371. << endl;
  372. fflush(stdout);
  373. _exit(0);
  374. }
  375.  
  376. waitpid(pid, nullptr, 0);
  377.  
  378. fclose(f);
  379. }
  380.  
  381. void genInts(FILE *f, int n) {
  382. while (n--)
  383. fprintf(f, "%i\n", (int)_r_gen());
  384. }
  385.  
  386. void genString(FILE *f, int n) {
  387. while (n--)
  388. fputc(getRandom('A', 'z'), f);
  389. fputc('\n', f);
  390. }
  391.  
  392. void genNothing(FILE*, int) {}
  393.  
  394. void benchmark(int n) {
  395. ios::sync_with_stdio(false);
  396. cin.tie(nullptr);
  397. cout << "n: " << n << " (" << (double)n << ")" << endl;
  398.  
  399. run(genInts, benchCinInt, n, "cin >> int");
  400. // run(genInts, benchFinInt, n, "fin >> int");
  401. run(genInts, benchScanfInt, n, "scanf(int)");
  402. cout << endl;
  403. run(genString, benchCinString, n, "cin >> string");
  404. // run(genString, benchFinString, n, "fin >> string");
  405. run(genString, benchCinCstring, n, "cin >> char*");
  406. // run(genString, benchFinCstring, n, "fin >> char*");
  407. run(genString, benchScanfCstring, n, "scanf(char*)");
  408. cout << endl;
  409. run(genNothing, benchCoutInt, n, "cout << int");
  410. run(genNothing, benchPrintfInt, n, "printf(int)");
  411. cout << endl;
  412. run(genNothing, benchCoutString, n, "cout << string");
  413. run(genNothing, benchCoutCstring, n, "cout << char*");
  414. run(genNothing, benchPrintfCstring, n, "printf(char*)");
  415. }
  416.  
  417. int main() {
  418. constexpr int N = 2e7;
  419. benchmark(N);
  420. return 0;
  421. }
  422.  
  423. /*
  424.  
  425. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement