Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the System.currentTimeMillis () method to calculate the running time of a program in Java

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to use the System.currentTimeMillis () method to calculate the running time of the program in Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

The System.currentTimeMillis () method provided in Java is used to get the current computer time, expressed in milliseconds between the current computer time and GMT time (Greenwich time) at 00:00:00 on January 1, 1970.

The return type of the System.currentTimeMillis () method is long, which represents the current time in milliseconds.

During the development process, many people are used to using new Date () to get the current time. What new Date () does is actually call the System.currentTimeMillis () method. If it is only required or milliseconds, then you can use System.currentTimeMillis () instead of new Date (), which is a little more efficient.

[example] calculate the time it takes to concatenate strings of String type and StringBuilder type.

/ * * Java uses the System.currentTimeMillis () method to calculate the run time of the program * @ author pan_junbiao * * / public class CurrentTimeTest {/ * it takes * / public static void testString () {String s = "Hello"; String S1 = "World"; long start = System.currentTimeMillis (); for (int item0; I String) to concatenate strings of type String.

In terms of thread safety, StringBuilder is not thread safe, while StringBuffer is thread safe.

These are all the contents of the article "how to use the System.currentTimeMillis () method to calculate the running time of a program in Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report