Wednesday, May 18, 2011

Optimizations on ACM Timus

Some tricks I use on

Note: ACM Timus uses MS VCPP compiler, which differs from g++.

1. c++ is much much more efficient than java.
2. Use short/char instead of int whenever possible (to save memory), esp for arrays. Of course, there is a potential speed / memory trade off here since 4 byte ints register-size, but it doesn't seem to be issue on ACM Timus.
3. When memory is an issue, use stdio instead of iostream.
4. Using iostream may be "faster" than stdio?
5. "Implicit" stack overflow solution:
Use #pragma comment(linker, "/STACK:16777216")
6. Struct packing (for arrays) may not be memory efficient.
7. STL saves coding time.
8. Useful: #define FOR(i,n) for(i=0;i<(n);i++)

Java Optimizations:
1. Many prints are slower than one single print.
2. Buffered Reader/Writer is faster than Scanner.

No comments: