Return-Path: X-Original-To: apmail-flex-dev-archive@www.apache.org Delivered-To: apmail-flex-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2E49010963 for ; Fri, 18 Oct 2013 01:18:25 +0000 (UTC) Received: (qmail 80708 invoked by uid 500); 18 Oct 2013 01:18:24 -0000 Delivered-To: apmail-flex-dev-archive@flex.apache.org Received: (qmail 80684 invoked by uid 500); 18 Oct 2013 01:18:24 -0000 Mailing-List: contact dev-help@flex.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flex.apache.org Delivered-To: mailing list dev@flex.apache.org Received: (qmail 80676 invoked by uid 99); 18 Oct 2013 01:18:24 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Oct 2013 01:18:24 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy includes SPF record at spf.trusted-forwarder.org) Received: from [203.59.1.218] (HELO icp-osb-irony-out2.external.iinet.net.au) (203.59.1.218) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Oct 2013 01:18:17 +0000 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApIBAC6LYFLKn6Lk/2dsb2JhbAANTYM/wCCDWme2eZMskneBBwOZNYUwjl0 X-IronPort-AV: E=Sophos;i="4.93,518,1378828800"; d="scan'208";a="153839699" Received: from unknown (HELO [192.168.0.3]) ([202.159.162.228]) by icp-osb-irony-out2.iinet.net.au with ESMTP; 18 Oct 2013 09:17:53 +0800 From: Justin Mclean Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: UID performance Date: Fri, 18 Oct 2013 12:17:53 +1100 Message-Id: To: dev@flex.apache.org Mime-Version: 1.0 (Apple Message framework v1283) X-Mailer: Apple Mail (2.1283) X-Virus-Checked: Checked by ClamAV on apache.org Hi, This is about 2/3rds to twice as fast by my measurements (in Scout). Any = one confirm? Changes include. - Use a string to store hex digits and charAt rather than array of ascii - Use Array.join rather than String.fromCharCode - Remove Math.floor as they are not required - Set up static array and reuse that rather than creating new array each = time The timeString could probably be improved to remove the string = manipulation but put the digits directly into the correct place into the = array to save another 5% or so. BYW any see where Number.toString is being called in there? Any feedback? Thanks, Justin diff --git a/frameworks/projects/framework/src/mx/utils/UIDUtil.as = b/frameworks/projects/framework/src/mx/utils/UIDUtil.as index 4993563..576b598 100644 --- a/frameworks/projects/framework/src/mx/utils/UIDUtil.as +++ b/frameworks/projects/framework/src/mx/utils/UIDUtil.as @@ -61,10 +61,16 @@ public class UIDUtil =20 /** * @private - * Char codes for 0123456789ABCDEF */ - private static const ALPHA_CHAR_CODES:Array =3D [48, 49, 50, 51, = 52, 53, 54,=20 - 55, 56, 57, 65, 66, 67, 68, 69, 70]; + private static const CHAR_CODES:String =3D "0123456789ABCDEF"; +=09 + private static var EMPTYUID:Array =3D [ + '0','0','0','0','0','0','0','0', + '-','0','0','0','0', + '-','0','0','0','0', + '-','0','0','0','0', + '0','0','0','0','0','0','0','0','0','0','0','0' + ]; =20 = //------------------------------------------------------------------------= -- // @@ -106,53 +112,53 @@ public class UIDUtil * @playerversion AIR 1.1 * @productversion Flex 3 */ - public static function createUID():String - { - var uid:Array =3D new Array(36); - var index:int =3D 0; - =20 - var i:int; - var j:int; - =20 - for (i =3D 0; i < 8; i++) - { - uid[index++] =3D ALPHA_CHAR_CODES[Math.floor(Math.random() = * 16)]; - } - - for (i =3D 0; i < 3; i++) - { - uid[index++] =3D 45; // charCode for "-" - =20 - for (j =3D 0; j < 4; j++) - { - uid[index++] =3D = ALPHA_CHAR_CODES[Math.floor(Math.random() * 16)]; - } - } - =20 - uid[index++] =3D 45; // charCode for "-" - - var time:Number =3D new Date().getTime(); - // Note: time is the number of milliseconds since 1970, - // which is currently more than one trillion. - // We use the low 8 hex digits of this number in the UID. - // Just in case the system clock has been reset to - // Jan 1-4, 1970 (in which case this number could have only - // 1-7 hex digits), we pad on the left with 7 zeros - // before taking the low digits. - var timeString:String =3D ("0000000" + = time.toString(16).toUpperCase()).substr(-8); - =20 - for (i =3D 0; i < 8; i++) - { - uid[index++] =3D timeString.charCodeAt(i); - } - =20 - for (i =3D 0; i < 4; i++) - { - uid[index++] =3D ALPHA_CHAR_CODES[Math.floor(Math.random() = * 16)]; - } - =20 - return String.fromCharCode.apply(null, uid); - } + public static function createUID():String + { + var uid:Array =3D EMPTYUID; + var index:int =3D 0; + =09 + var i:int; + var j:int; + =09 + for (i =3D 0; i < 8; i++) + { + uid[index++] =3D CHAR_CODES.charAt(Math.random() = * 16); + } + =09 + for (i =3D 0; i < 3; i++) + { + index++; // skip "-" + =09 + for (j =3D 0; j < 4; j++) + { + uid[index++] =3D = CHAR_CODES.charAt(Math.random() * 16); + } + } + =09 + index++; // skip "-" + =09 + var time:Number =3D new Date().getTime(); + // Note: time is the number of milliseconds since 1970, + // which is currently more than one trillion. + // We use the low 8 hex digits of this number in the = UID. + // Just in case the system clock has been reset to + // Jan 1-4, 1970 (in which case this number could have = only + // 1-7 hex digits), we pad on the left with 7 zeros + // before taking the low digits. + var timeString:String =3D ("0000000" + = time.toString(16).toUpperCase()).substr(-8); + =09 + for (i =3D 0; i < 8; i++) + { + uid[index++] =3D timeString.charAt(i); + } + =09 + for (i =3D 0; i < 4; i++) + { + uid[index++] =3D CHAR_CODES.charAt(Math.random() = * 16); + } + =09 + return uid.join(""); + }