commons-lang的FastDateFormat性能測試

commons-lang的FastDateFormat是一個thread-safe的,對SimpleDateFormat的一個重新實現。

SimpleDateFormat為什么不是thread-safe的呢?看一下具體實現就知道了,其父類中定義了成員變量Calendar,每次格式化日期時都會先重置這個Calendar的時間:calendar.setTime(date),這樣多個線程不就出問題了。
而FastDateFormat是thread-safe的,但從其源代碼看解決方案也很直接,每次格式化時都new一個Calendar對象出來,每次咱都用新的不就行了。單從這方面看FastDateFormat還真沒Fast多少,但FastDateFormat比SimpleDateFormat還是先進了一點,對于同一種格式化日期的pattern,FastDateFormat可以保證只有一個實例產生,實現了對pattern的管理。
FastDateFormat的這種方式還是會帶來一些問題,比如從緩存中獲取pattern時在多個線程間的同步問題。從commons-lang2.6的源代碼中看到下面的方法:
public static synchronized FastDateFormat getInstance(String pattern, TimeZone timeZone, Locale locale)
可見大并發下還是會有鎖等待的(當時為什么沒有使用Double-check方式呢?)。commons-lang3.3對這部分做了優化,使用了ConcurrentHashMap作為緩存。
下面是我做的一些測試,
測試程序:
Case1:使用FastDateFormat
    public static long currentSystemTimeMillis() {
FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMddHHmmss");
return Long.parseLong(fdf.format(System.currentTimeMillis()));
}
?
Case2:直接使用Calendar
    public static long currentSystemTimeMillis() {
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(new Date(System.currentTimeMillis()));
int year = rightNow.get(Calendar.YEAR);
int month = rightNow.get(Calendar.MONTH) + 1;
int day = rightNow.get(Calendar.DAY_OF_MONTH);
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int minute = rightNow.get(Calendar.MINUTE);
int second = rightNow.get(Calendar.SECOND);
String strDateTime =
year
+ (month < 10 ? "0" + month : month + "")
+ (day < 10 ? "0" + day : day + "")
+ (hour < 10 ? "0" + hour : hour + "")
+ (minute < 10 ? "0" + minute : minute + "")
+ (second < 10 ? "0" + second : second + "");
return Long.parseLong(strDateTime);
}
//測試主方法
    public static void testDateFormat() throws Exception {
System.out.println("Begin test of currentSystemTimeMillis()");
System.out.println("currentSystemTimeMillis:"+currentSystemTimeMillis());
int tCnt = 50;
Thread[] threads = new Thread[tCnt];
for (int i = 0; i < tCnt; i++) {
Runnable run = new Runnable() {
public void run() {
try {
int runCounter = 0;
for (long i = 0; i < 100000l; i++) {
currentSystemTimeMillis();
runCounter++;
}
System.out.println(Thread.currentThread().getName()
+ " finished. runCounter="
+ runCounter);
} catch (Exception e) {
}
}
};
threads[i] = new Thread(run, "Thread" + i);
}
long start = System.currentTimeMillis();
for (int i = 0; i < tCnt; i++) {
threads[i].start();
}
for (int i = 0; i < tCnt; i++) {
threads[i].join();
}
System.out.println("Test ended cost:" + (System.currentTimeMillis() - start));
}
測試環境:
CPU: AMD Phenom II 4核 3.4GHz
RAM: 4GB
OS : WINDOWS 7
50個線程,每個線程調用10萬次
測試結果(程序總共執行耗時):
JVM參數:-Xms512m -Xmx512m
FastDateFormat ?: 5078ms
直接使用Calendar:?3947ms
JVM參數:-server -Xms512m -Xmx512m
FastDateFormat ?: 2716ms
直接使用Calendar: 2285ms
可見在純粹的速度上FastDateFormat要比直接使用Calendar要慢,但對于服務器程序來說,開啟-server后,差距會縮小,由于是在windows上測試的,開不開-server只是和是否使用Parallel GC有關(開啟后Parallel GC很好地利用了4核cpu的優勢,減少了一部分GC時間)。又由于本次并發量很大,所以可以預見在實際應用中,對于服務器程序而言,使用FastDateFormat后,性能影響應該不是很大。

轉載于:https://www.cnblogs.com/snowboyovo/archive/2012/02/28/2371200.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379916.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379916.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379916.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C——Flowers

Problem Description As you know, Gardon trid hard for his love-letter, and now he’s spending too much time on choosing flowers for Angel. When Gardon entered the flower shop, he was frightened and dazed by thousands kinds of flowers. “How can I choose!”…

include函數_include()函數以及JavaScript中的示例

include函數includes() is a predefined function in JavaScript, which is used to check whether a given element exists in the array or not? include()是JavaScript中的預定義函數&#xff0c;用于檢查數組中是否存在給定元素&#xff1f; Example: 例&#xff1a; &l…

利用POI創建OpenOffice中的Excel文件

之所以稱為OpenOffice的Excel文件,我發現了一個特點就是: 一些網站嚴格限定了文件必須為MS的Excel格式的話,用POI的HSSF創建的Excel就會不識別.不知道是什么原因,可能是版本的問題,據說HSSF(令人討厭的電子表格格式)生成的是MS97的格式.但是97-2003的提示中明顯的說明了MS的lib…

批處理文章集錦

http://www.5dmail.net/html/2005-10-17/20051017181702.htmhttp://www.cnblogs.com/glaivelee/archive/2009/10/07/1578737.html轉載于:https://www.cnblogs.com/Jessy/archive/2012/02/29/2372955.html

web安全-----CSRF漏洞

簡述 CSRF&#xff1a;Cross-site request -forgery&#xff0c;跨站請求偽造&#xff0c;是一種web攻擊方式&#xff0c;是由于網站的cookie在瀏覽器中不會過期&#xff0c;只要不關閉瀏覽器或者退出登錄&#xff0c;那以后只要訪問這個網站&#xff0c;都會默認你已經登錄。…

java math 類_Java Math類靜態長輪(double d)示例

java math 類數學課靜態長回合(雙D) (Math Class static long round(double d) ) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to return the closest long value to the given argument. 此方法用于將最接近的long值返回…

C——求平均成績

Problem Description 假設一個班有n(n<50)個學生&#xff0c;每人考m(m<5)門課&#xff0c;求每個學生的平均成績和每門課的平均成績&#xff0c;并輸出各科成績均大于等于平均成績的學生數量。 Input 輸入數據有多個測試實例&#xff0c;每個測試實例的第一行包括兩個…

依賴、關聯、聚合、組合還有泛化的關系(轉載)

依賴、關聯、聚合、組合還有泛化的關系 此文為轉載文章:http://zjzkiss.cnblogs.com/世界是普遍聯系的&#xff0c;因此程序世界中的類&#xff0c;也不可能是孤立的。UML為我們定義了它們之間的關系&#xff0c;就是&#xff1a;依賴、關聯、聚合、組合還有泛化。 泛化關系比…

神奇的LINQ ---可以通過對象來查詢數據

摘要&#xff1a; linq:在一個新項目里面要用這個技術&#xff0c;然后自己拿起書看了下&#xff0c;記錄下自己的新發現&#xff0c;只適合簡單入門的新童鞋看呀&#xff01;&#xff01;&#xff01; 結論&#xff1a; linq是對象領域與數據領域的一個橋梁。 為什么會出現Lin…

java math.cos_Java Math類靜態double cos(double d)示例

java math.cos數學類靜態雙cos(double d) (Math Class static double cos(double d)) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to return the trigonometric cosine of an angle of the given parameter in the meth…

web安全---SSRF漏洞

簡介 SSRF&#xff1a;服務器請求偽造&#xff0c;是一種攻擊者構造形成由服務端發起請求 的一個安全漏洞。一般情況下&#xff0c;SSRF攻擊的目標是從外網無法訪問的內部系統&#xff08;正是因為它是由服務端發起的&#xff0c;所以它能夠請求到與它相連而與外網隔離的內部系…

集合——對象數組(引用數據類型數組)

案例&#xff1a;我有5個學生&#xff0c;請把這個5個學生的信息存儲到引用數據類型數組中&#xff0c;并遍歷數組&#xff0c;獲取得到每一個學生的信息。 思路分析&#xff1a;首先&#xff0c;想要創建學生對象&#xff0c;就得有學生這個類&#xff0c;所以&#xff0c;首…

提升應用視覺Android效果的10個UI技巧

在Android應用開發中&#xff0c;風格和設計或許不是最關鍵的要素&#xff0c;但它們在決定Android應用成功與否上確實扮演重要的角色。以下是10個Android應用的UI設計技巧&#xff0c;還有個附加技巧&#xff0c;能夠提供你的Android應用的視覺吸引力。 技巧1&#xff1a;使用…

kotlin中判斷字符串_Kotlin程序查找字符串中字符的頻率

kotlin中判斷字符串Given a string and a character, we have to find the frequency of the character in the string. 給定一個字符串和一個字符&#xff0c;我們必須找到字符串中字符的頻率。 Example: 例&#xff1a; Input:string "IncludeHelp"character to…

OD使用

0x01 功能界面 序號1是匯編代碼對應的地址窗口序號2是匯編對應的十六進制機器碼窗口序號3是反匯編窗口序號4是反匯編代碼對應的注釋信息窗口序號5是寄存器信息窗口序號6是當前執行到的反匯編代碼的信息窗口序號7是數據所在的地址序號8是數據的十六進制編碼信息&#xff0c;序號…

windows mobile 開發總結--菜單

在開發時經常要創建菜單&#xff0c;并且動態顯示和隱藏菜單或者是某個子菜單。以下就是實現的方法&#xff1a; 1。創建并顯示菜單,先在資源里添加菜單&#xff0c;然后如下代碼 SHMENUBARINFO mbi; ZeroMemory(&mbi, sizeof(SHMENUBARINFO)); mbi.cbSizesizeof(SHMENUBAR…

Java——集合的概述

* A&#xff1a;集合的由來* 數組是容器&#xff0c;集合也是容器* 數組的弊端&#xff1a;數組的長度是固定的&#xff0c;當添加的元素超過了數組的長度時&#xff0c;需要對數組重新定義&#xff0c;太麻煩* Java內部給我們提供了集合類&#xff0c;可以存儲任意對象&#x…

排序算法中平均時間復雜度_操作系統中的作業排序(算法,時間復雜度和示例)...

排序算法中平均時間復雜度作業排序 (Job sequencing) Job sequencing is the set of jobs, associated with the job i where deadline di > 0 and profit pi > 0. For any job i the profit is earned if and only if the job is completed by its deadline. To complet…

python---文件處理

0x01 打開一個文件 python中內置了文件對象&#xff0c;通過open()函數就可以制定模式打開指定文件&#xff0c;并創建文件對象。該函數的格式如下&#xff1a; open(file[,moder[,buffering-1]])file&#xff1a;指定要打開或創建的文件名稱&#xff0c;如果該文件不存在當前…

簡易而又靈活的Javascript拖拽框架(四)

一、開篇 似乎拖拽已經被寫爛了&#xff0c;沒得寫的了&#xff0c;可是我這次又來了&#xff5e; 上一次寫的是跨列拖放&#xff0c;這次我要帶給大家的是跨頁拖放。 可以到這里來看看效果&#xff1a;示例效果 說明&#xff1a;1、如果將方框拖動到頁簽上立刻釋放掉的話&…