1. 程式人生 > 其它 >JAVA 使用系統時間製作簡單的計時器

JAVA 使用系統時間製作簡單的計時器

獲取當前系統時間:

long startTime = System.currentTimeMillis();

計時器:開始的位置獲取一次時間,結束的時候獲取一次時間,用:

endTime - stratTime;

舉例:

預設JDK配置下測試StringBuffer和StringBuilder的效率:

PS D:\DEV\code\java\java101> java -version
openjdk version "1.8.0_322"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_322-b06)
OpenJDK 64-Bit Server VM (Temurin)(build 25.322
-b06, mixed mode)

測試用程式碼:

import java.util.Properties;
import java.util.Random;

public class TestWithTimer {

    public static void main(String[] args) {

        
        Properties p = System.getProperties();
        String[] p_formated = p.toString().split(",");
        for (String pstr : p_formated) {
            System.out.println(pstr);
        }
        

        
        
int testLoopCtlForeachTestTask = 1_000_000_000; int totalTestLoopCtl = 10; long timer_sum_buffer = 0; long timer_sum_builder = 0; //String testStr = "1234567890"; long startTime = 0L; long endTime = 0L; for (int totalTestLoopCtlInLoop = 0; totalTestLoopCtlInLoop < totalTestLoopCtl; totalTestLoopCtlInLoop++) { Random rd
= new Random(); String testStr = String.valueOf(rd.nextLong()); startTime = System.currentTimeMillis(); for (int i = 0; i < testLoopCtlForeachTestTask; i++) { StringBuffer sbuf = new StringBuffer(testStr).reverse(); } endTime = System.currentTimeMillis(); long sBuffer_timer_delta = endTime - startTime; timer_sum_buffer+=sBuffer_timer_delta; System.out.println("stringBufferFromStringTest:" + (sBuffer_timer_delta)); startTime = System.currentTimeMillis(); for (int i = 0; i < testLoopCtlForeachTestTask; i++) { StringBuilder sb = new StringBuilder(testStr); } endTime = System.currentTimeMillis(); long sBiulder_timer_delta = endTime - startTime; timer_sum_builder+=sBiulder_timer_delta; System.out.println("stringBuilderFromStringTest:" + (sBiulder_timer_delta)); System.out.println("-----------"+totalTestLoopCtlInLoop+"--------------"); } double timer_sum_buffer_double = timer_sum_buffer; double timer_sum_builder_double = timer_sum_builder; System.out.println("TestResult is:\n sBuffer 1Billion:"+(timer_sum_buffer_double/totalTestLoopCtl)+"\n sBuilder 1Billion:"+(timer_sum_builder_double/totalTestLoopCtl)); } }

 

輸出:

{java.runtime.name=OpenJDK Runtime Environment
 sun.boot.library.path=C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\bin
 java.vm.version=25.322-b06
 user.country.format=CN
 java.vm.vendor=Temurin
 java.vendor.url=https://adoptium.net/
 path.separator=;
 java.vm.name=OpenJDK 64-Bit Server VM
 file.encoding.pkg=sun.io
 user.country=US
 user.script=
 sun.java.launcher=SUN_STANDARD
 sun.os.patch.level=
 java.vm.specification.name=Java Virtual Machine Specification
 user.dir=D:\DEV\code\java\java101
 java.runtime.version=1.8.0_322-b06
 java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
 java.endorsed.dirs=C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\endorsed
 os.arch=amd64
 java.io.tmpdir=C:\Users\avatu\AppData\Local\Temp\
 line.separator=

 java.vm.specification.vendor=Oracle Corporation
 user.variant=
 os.name=Windows 10
 sun.jnu.encoding=GBK
 java.library.path=C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python310\Scripts\;C:\Program Files\Python310\;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Users\avatu\.cargo\bin;C:\Users\avatu\AppData\Local\Microsoft\WindowsApps;C:\Users\avatu\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\avatu\.dotnet\tools;C:\Users\avatu\AppData\Local\GitHubDesktop\bin;.
 java.specification.name=Java Platform API Specification
 java.class.version=52.0
 sun.management.compiler=HotSpot 64-Bit Tiered Compilers
 os.version=10.0
 user.home=C:\Users\avatu
 user.timezone=
 java.awt.printerjob=sun.awt.windows.WPrinterJob
 file.encoding=GBK
 java.specification.version=1.8
 java.class.path=C:\Users\avatu\AppData\Roaming\Code\User\workspaceStorage\d9cb4d9bacc23ff8149200f35a590a43\redhat.java\jdt_ws\java101_6f898212\bin
 user.name=avatu
 java.vm.specification.version=1.8
 sun.java.command=TestWithTimer
 java.home=C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre
 sun.arch.data.model=64
 user.language=en
 java.specification.vendor=Oracle Corporation
 user.language.format=zh
 awt.toolkit=sun.awt.windows.WToolkit
 java.vm.info=mixed mode
 java.version=1.8.0_322
 java.ext.dirs=C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
 sun.boot.class.path=C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\resources.jar;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\rt.jar;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\sunrsasign.jar;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\jsse.jar;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\jce.jar;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\charsets.jar;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\lib\jfr.jar;C:\Program Files\Eclipse Adoptium\jdk-8.0.322.6-hotspot\jre\classes
 java.vendor=Temurin
 sun.stderr.encoding=ms936
 file.separator=\
 java.vendor.url.bug=https://github.com/adoptium/adoptium-support/issues
 sun.io.unicode.encoding=UnicodeLittle
 sun.cpu.endian=little
 sun.desktop=windows
 sun.cpu.isalist=amd64}
stringBufferFromStringTest:27703
stringBuilderFromStringTest:18398
-----------0--------------
stringBufferFromStringTest:28843
stringBuilderFromStringTest:19523
-----------1--------------
stringBufferFromStringTest:28962
stringBuilderFromStringTest:19580
-----------2--------------
stringBufferFromStringTest:27225
stringBuilderFromStringTest:19677
-----------3--------------
stringBufferFromStringTest:27278
stringBuilderFromStringTest:19753
-----------4--------------
stringBufferFromStringTest:27299
stringBuilderFromStringTest:19709
-----------5--------------
stringBufferFromStringTest:28956
stringBuilderFromStringTest:19531
-----------6--------------
stringBufferFromStringTest:28976
stringBuilderFromStringTest:19539
-----------7--------------
stringBufferFromStringTest:28896
stringBuilderFromStringTest:19492
-----------8--------------
stringBufferFromStringTest:27697
stringBuilderFromStringTest:19718
-----------9--------------
TestResult is:
 sBuffer 1Billion:28183.5
 sBuilder 1Billion:19492.0