iwiwi 備忘録

学んだことを殴り書きます。自分向けのメモです。

高速入出力

ifstream で読むより4倍ぐらい速い感じ

inline bool readeof(FILE *fp) {
  for (;;) {
    int c = getc(fp);
    if (c == EOF) {
      return true;
    } else if (isspace(c)) {
      continue;
    } else {
      ungetc(c, fp);
      return false;
    }
  }
}

inline int readint(FILE *fp) {
  int c, x, s;
  while (!isdigit(c = getc(fp)) && c != '-');
  if (c == '-') {
    s = -1;
    x = 0;
  } else {
    assert(isdigit(c));
    s = 1;
    x = c - '0';
  }
  while (isdigit(c = getc(fp))) {
    x = (x * 10 + (c - '0'));
  }
  return s * x;
}