Using currentTimeMillis( ) to Time Program Execution
One use of theSystemclass that you might find particularly interesting is to use the
currentTimeMillis( )method to time how long various parts of your program take to execute.
ThecurrentTimeMillis( )method returns the current time in terms of milliseconds since
midnight, January 1, 1970. To time a section of your program, store this value just before
beginning the section in question. Immediately upon completion, callcurrentTimeMillis( )
again. The elapsed time will be the ending time minus the starting time. The following
program demonstrates this:
// Timing program execution.
class Elapsed {
public static void main(String args[]) {
long start, end;
System.out.println("Timing a for loop from 0 to 1,000,000");
// time a for loop from 0 to 1,000,000
410 Part II: The Java Library
Method Description
static Channel inheritedChannel( )
throws IOException
Returns the channel inherited by the Java Virtual
Machine. Returnsnullif no channel is inherited.
static void load(Stringlibrar yFileName) Loads the dynamic librar y whose file is specified by
librar yFileName,which must specify its complete path.
static void loadLibrar y(Stringlibrar yName) Loads the dynamic librar y whose name is
associated withlibrar yName.
static String mapLibrar yName(Stringlib) Returns a platform-specific name for the librar y
namedlib.
static long nanoTime( ) Obtains the most precise timer in the system and
returns its value in terms of nanoseconds since
some arbitrar y starting point. The accuracy of the
timer is unknowable.
static void runFinalization( ) Initiates calls to thefinalize( )methods of unused
but not yet recycled objects.
static void setErr(PrintStreameStream) Sets the standarderrstream toeStream.
static void setIn(InputStreamiStream) Sets the standardinstream toiStream.
static void setOut(PrintStreamoStream) Sets the standardoutstream tooStream.
static void
setProperties(PropertiessysProperties)
Sets the current system properties as specified by
sysProperties.
static String setProperty(Stringwhich,
Stringv)
Assigns the valuevto the property namedwhich.
static void setSecurityManager(
SecurityManagersecMan)
Sets the security manager to that specified by
secMan.
TABLE 16-13 The Methods Defined bySystem(continued)