Small speedups in time-related code
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 9 Dec 2008 16:20:32 +0000 (16:20 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Tue, 9 Dec 2008 16:20:32 +0000 (16:20 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@780 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/src/com/google/zxing/client/android/DecodeThread.java
android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java
android/src/com/google/zxing/client/android/result/CalendarResultHandler.java
android/src/com/google/zxing/client/android/result/ResultHandler.java

index f2ad73b..d49f052 100755 (executable)
@@ -28,7 +28,6 @@ import com.google.zxing.MultiFormatReader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
 
-import java.util.Date;
 import java.util.Hashtable;
 import java.util.Vector;
 
@@ -151,7 +150,7 @@ final class DecodeThread extends Thread {
    * @param height The height of the preview frame.
    */
   private void decode(byte[] data, int width, int height) {
-    Date startDate = new Date();
+    long start = System.currentTimeMillis();
     boolean success;
     Result rawResult = null;
     YUVMonochromeBitmapSource source = new YUVMonochromeBitmapSource(data, width, height,
@@ -162,18 +161,18 @@ final class DecodeThread extends Thread {
     } catch (ReaderException e) {
       success = false;
     }
-    Date endDate = new Date();
+    long end = System.currentTimeMillis();
 
     if (success) {
       Message message = Message.obtain(mActivity.mHandler, R.id.decode_succeeded, rawResult);
-      message.arg1 = (int) (endDate.getTime() - startDate.getTime());
+      message.arg1 = (int) (end - start);
       Bundle bundle = new Bundle();
       bundle.putParcelable(BARCODE_BITMAP, source.renderToBitmap());
       message.setData(bundle);
       message.sendToTarget();
     } else {
       Message message = Message.obtain(mActivity.mHandler, R.id.decode_failed);
-      message.arg1 = (int) (endDate.getTime() - startDate.getTime());
+      message.arg1 = (int) (end - start);
       message.sendToTarget();
     }
   }
index 84afab5..20bef0e 100644 (file)
@@ -32,6 +32,8 @@ import java.util.Date;
 
 public final class AddressBookResultHandler extends ResultHandler {
 
+  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
+
   private final boolean[] mFields;
   private int mButtonCount;
 
@@ -145,8 +147,10 @@ public final class AddressBookResultHandler extends ResultHandler {
 
     String birthday = result.getBirthday();
     if (birthday != null && birthday.length() > 0) {
-      DateFormat format = new SimpleDateFormat("yyyyMMdd");
-      Date date = format.parse(birthday, new ParsePosition(0));
+      Date date;
+      synchronized (DATE_FORMAT) {
+        date = DATE_FORMAT.parse(birthday, new ParsePosition(0));
+      }
       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
     }
     ParsedResult.maybeAppend(result.getNote(), contents);
index 82d5fa9..4625417 100644 (file)
@@ -30,6 +30,9 @@ import java.util.GregorianCalendar;
 
 public final class CalendarResultHandler extends ResultHandler {
 
+  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
+  private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
+
   private static final int[] mButtons = {
       R.string.button_add_calendar
   };
@@ -77,13 +80,17 @@ public final class CalendarResultHandler extends ResultHandler {
   private void appendTime(String when, StringBuffer result) {
     if (when.length() == 8) {
       // Show only year/month/day
-      DateFormat format = new SimpleDateFormat("yyyyMMdd");
-      Date date = format.parse(when, new ParsePosition(0));
+      Date date;
+      synchronized (DATE_FORMAT) {
+        date = DATE_FORMAT.parse(when, new ParsePosition(0));
+      }
       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
     } else {
       // The when string can be local time, or UTC if it ends with a Z
-      DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
-      Date date = format.parse(when.substring(0, 15), new ParsePosition(0));
+      Date date;
+      synchronized (DATE_TIME_FORMAT) {
+       date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
+      }
       long milliseconds = date.getTime();
       if (when.length() == 16 && when.charAt(15) == 'Z') {
         Calendar calendar = new GregorianCalendar();
index 2e49aa9..ffbdf81 100644 (file)
@@ -37,6 +37,9 @@ import java.util.GregorianCalendar;
 
 public abstract class ResultHandler {
 
+  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
+  private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
+
   public static final int MAX_BUTTON_COUNT = 4;
 
   protected final ParsedResult mResult;
@@ -119,13 +122,17 @@ public abstract class ResultHandler {
   private long calculateMilliseconds(String when) {
     if (when.length() == 8) {
       // Only contains year/month/day
-      DateFormat format = new SimpleDateFormat("yyyyMMdd");
-      Date date = format.parse(when, new ParsePosition(0));
+      Date date;
+      synchronized (DATE_FORMAT) {
+        date = DATE_FORMAT.parse(when, new ParsePosition(0));
+      }
       return date.getTime();
     } else {
       // The when string can be local time, or UTC if it ends with a Z
-      DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
-      Date date = format.parse(when.substring(0, 15), new ParsePosition(0));
+      Date date;
+      synchronized (DATE_TIME_FORMAT) {
+       date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
+      }
       long milliseconds = date.getTime();
       if (when.length() == 16 && when.charAt(15) == 'Z') {
         Calendar calendar = new GregorianCalendar();