001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.issue.lbclassic36; 015 016import java.text.SimpleDateFormat; 017import java.util.Date; 018 019//import org.joda.time.format.DateTimeFormat; 020//import org.joda.time.format.DateTimeFormatter; 021 022public class DateFormatPerf_Tapp { 023 public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS"; 024 static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L; 025 026 static long RUN_LENGTH = 1000 * 1000; 027 028 public static void main(String[] args) { 029 for (int i = 0; i < 5; i++) { 030 doRawJoda(); 031 doRawSDF(); 032 } 033 034 print("Raw Joda: ", doRawJoda()); 035 print("Raw SDF: ", doRawSDF()); 036 } 037 038 static void print(String msg, double avg) { 039 System.out.println(msg + " average tick " + avg + " nanoseconds"); 040 } 041 042 static double doRawJoda() { 043 // DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN); 044 @SuppressWarnings("unused") 045 long timeInMillis = new Date().getTime(); 046 long start = System.nanoTime(); 047 for (int i = 0; i < RUN_LENGTH; ++i) { 048 // jodaFormat.print(timeInMillis); 049 } 050 return (System.nanoTime() - start) * 1.0d / RUN_LENGTH; 051 } 052 053 static double doRawSDF() { 054 SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN); 055 long timeInMillis = new Date().getTime(); 056 long start = System.nanoTime(); 057 for (int i = 0; i < RUN_LENGTH; ++i) { 058 simpleFormat.format(timeInMillis); 059 } 060 return (System.nanoTime() - start) * 1.0d / RUN_LENGTH; 061 } 062 063}