Java--System.currentTimeMillis() calculates the operating efficiency of the code by obtaining the system time

New year, new beginning, let's work hard together!
Okay, let's get to the main topic: calculate the operating efficiency (time difference) of the code by obtaining the system time.
First, we insert the code for obtaining the system time before running at the beginning, and assign the obtained result to the long starttime.

//获取代码执行前的系统时间
        long startTime = System.currentTimeMillis();

Then insert this piece of code at the end of the code, and assign the time to the long end.

//获取代码执行结束后的系统时间
        long endTime = System.currentTimeMillis();

Then we subtract the start time (starttTime-endTime) from the end time to get the running time, in milliseconds.

  //long类型计算时间差,单位毫秒
        long timeLong = endTime - startTime;

what? The millisecond is too complicated to understand? Then we will switch!

//long类型时间差转为double类型时间差,单位毫秒
        double timeDouble= Double.parseDouble(Long.toString(timeLong));
        System.out.println("该方法执行时间为" + timeDouble
        + "毫秒,即" + timeDouble/(double)1000 + "秒");

Get it done! Friends who like it, remember to pay attention and like it, let's make progress together!

Guess you like

Origin blog.csdn.net/qq_30068165/article/details/112060208