Skip to content

Article on common I/O optimizations #903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jxu opened this issue Jul 14, 2022 · 12 comments · May be fixed by #1383
Open

Article on common I/O optimizations #903

jxu opened this issue Jul 14, 2022 · 12 comments · May be fixed by #1383

Comments

@jxu
Copy link
Contributor

jxu commented Jul 14, 2022

It's well-known for competition programming in C++ to use the following simple I/O optimizations:

  • ios_base::sync_with_stdio(false) which shouldn't ever be a problem as you shouldn't use C I/O in c++ ever
  • cin.tie(0) (except for maybe interactive problems)

(they should be prefixed with std:: but competition programmers use namespace std which is bad style but probably ok for cp)

Here's an ancient Dr Dobbs article on it https://www.drdobbs.com/the-standard-librarian-iostreams-and-std/184401305
also https://codeforces.com/blog/entry/6251

Should this be its own article? So we have a convenient reference instead of tracking down CF blogs.

@jakobkogler
Copy link
Member

It would be part of #388, if that one becomes reality.

Other than that attempt at a translation, we don't have any C++ tutorial / guideline at all. And there are lots of other C++ related topics, that could add. E.g. when to use map/unordered map, policy-based data structures, interactive problems, those tricks like including bits/stdc++.h, avoiding hacking attempts with hashing, ...

I'm open to adding those to the site, if somebody takes the effort and writing up articles.

@jxu
Copy link
Contributor Author

jxu commented Jul 14, 2022

What about having this short article for now and maybe expanding it to C++ in CP later? That's a quite broad topic

@jakobkogler
Copy link
Member

Sure, why not.

@jxu
Copy link
Contributor Author

jxu commented Jul 20, 2022

Also I've heard that including bits/stdc++.h would increase compilation times since you are asking the compiler to parse and decide what to include from every single header file. Also it's not portable and you should have your imports in a code template anyway. I know people do it anyway, like using namespace std, but that could cause silly problems as well.

@jakobkogler
Copy link
Member

  • Compile time: It's possible to precompile the bits/stdc++.h, and you probably will gain some speed using it. But also with it, the compile time is not that terrible.
  • Portable: It's pretty portable. It should works for every g++ installation. On Linux, Windows, Mac.

I'm generally in favor of using bits/stdc++.h. Just because it avoids a huge template with 30+ includes.
And using namespace std is completely fine in competitive programming (in opposite to any real-life projects with dependencies).

@jxu
Copy link
Contributor Author

jxu commented Jul 24, 2022

Portability I mean some people on Windows may be using Microsoft compiler or clang on other platforms. Anyway I digress on those code-saving tricks

@jxu
Copy link
Contributor Author

jxu commented Jul 31, 2022

I've also come across some people using #define int long long. I was pretty sure this would break something in libraries but then I thought int is only required only to be 16-bits and I don't think any library would mind it being at least 64 bits (with signed main to get around main required to have type int). I haven't decided if this is a travesty or reasonable but I am tempted by the devil.

@adamant-pwn
Copy link
Member

I occasionally use #define int int64_t, though generally suggested practice is using int64_t directly. I kind of think that it's easier to code this way, though some other side effects include not being able to write min(0, x) for int x, because 0 is of type int, and some compilers wouldn't even allow min(0LL, x), because they think long long and int64_t are different types.

Another caveat is that on some compilers it might require you to use twice as much memory when you don't need it or make your execution time slower, so one should wary of using it in time/memory critical environment. Other than that, my personal opinion is it's fine to use the define, as long as you understand what you're doing.

P.S. Do I understand correctly that you're working on the article on this topic right now?

@jxu
Copy link
Contributor Author

jxu commented Aug 4, 2022

Thanks for your response. I can quickly whip up an article just to have some info on it

@adamant-pwn
Copy link
Member

There was a pull request with this in #939, but @adamsoltan withdrew it...

@jxu
Copy link
Contributor Author

jxu commented Dec 20, 2022

If the old PR is not going to be revived, I can whip up a quick article

@jxu jxu linked a pull request Oct 24, 2024 that will close this issue
@jxu
Copy link
Contributor Author

jxu commented Oct 24, 2024

Should psychological tips be added too? Like in an Um_Nik blog: stop worrying about rating, stop obsessing over saving time typing, master the fundamentals before learning advanced data structures which don't appear in Div2 and 3 contests

https://codeforces.com/blog/entry/92248
https://codeforces.com/blog/entry/113785

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants