java 的 System 和 Runtime
java的System获得系统环境信息以及java信息。java的Runtime获得运行时信息。而且这些函数的运行效率都超高,执行时间都在纳秒级。函数代码如下
?
获得环境信息
?
private static void getEnv() {for (Map.Entry<String, String> enviromentPara : System.getenv().entrySet())System.out.println(enviromentPara.getKey() + ":\t" + enviromentPara.getValue());long startTime = System.currentTimeMillis();int runCount = 1000 * 1000;String property = "NUMBER_OF_PROCESSORS";for (int i = 0; i < runCount; i++) {System.getenv(property);}long endTime = System.currentTimeMillis();System.out.println(property + ":" + System.getenv(property) + "\t" + (endTime - startTime) / (double)runCount + "mills");}?
获得java信息
?
private static void getProperties() {for (Map.Entry<Object, Object> property : System.getProperties().entrySet())System.out.println(property.getKey() + ":\t" + property.getValue());System.out.println(System.getProperty("java.version"));long startTime = System.currentTimeMillis();int runCount = 1000 * 1000;String property = "java.class.path";for (int i = 0; i < runCount; i++) {System.getProperty(property);}long endTime = System.currentTimeMillis();System.out.println(property + ":" + System.getProperty(property) + "\t" + (endTime - startTime) / (double)runCount + "mills");}
?
获得RunTime信息
?
private static void getRuntime() {Runtime runTimeInfo = Runtime.getRuntime();runTimeInfo.freeMemory();runTimeInfo.availableProcessors();runTimeInfo.maxMemory();runTimeInfo.totalMemory();long startTime = System.currentTimeMillis();int runCount = 1000 * 1000;for (int i = 0; i < runCount; i++) {runTimeInfo.totalMemory();}long endTime = System.currentTimeMillis();System.out.println("totalMemory:" + runTimeInfo.totalMemory()+ "\t" + (endTime - startTime) / (double)runCount + "mills");}?
?