A general method for constructing finite difference schemes for longtime integration problems is presented. It is demonstrated for discretizations of first and second space derivatives; however, the approach is not limited to these cases. The schemes are constructed so as to minimize the global truncation error, taking into account the initial data. The resulting second-order compact schemes can be used for integration times fourfold or more longer than previously studied schemes with similar computational complexity. A similar approach was used to obtain improved integration schemes. (C) 1994 Academic Press, Inc.