Difference between System.currentTimeMillis() and Date getTime()?

前端 未结 3 2110
故里飘歌
故里飘歌 2021-02-14 03:08

I was hoping to squeeze a tiny performance gain out of many calls to a function that returns a timestamp. The function looks like this:

public static long get_n         


        
3条回答
  •  臣服心动
    2021-02-14 04:03

    I would suggest running a unit test (ex. https://gist.github.com/ledlogic/8532028). I saw only a slight overall benefit to running the System.currentTimeMillis versus the (new Date()).getTime().

    1 billion runs: (1000 outer loops, 1,000,000 inner loops):
        System.currentTimeMillis(): 14.353 seconds
        (new Date()).getTime(): 16.668 seconds
    

    Individual runs would sometimes be slightly biased toward the later approach - depending on your system activity.

提交回复
热议问题