flex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maurice Amsellem <maurice.amsel...@systar.com>
Subject RE: UID performance
Date Fri, 18 Oct 2013 07:06:20 GMT
Here is the code for the test:

public class UIDUtilTest
{

    static private const COUNT:int = 100000;

    [Test]
    public function benchCallNCreateUID_Original():void
    {

        var res:String;
        for (var i:int = 0; i < COUNT; i++)
            res = UIDUtil_Orig.createUID();
    }

    [Test]
    public function benchCallNCreateUID_JMcLean():void
    {

        var res:String;
        for (var i:int = 0; i < COUNT; i++)
            res = UIDUtil_JML.createUID();
    }
}

-----Message d'origine-----
De : Maurice Amsellem [mailto:maurice.amsellem@systar.com] 
Envoyé : vendredi 18 octobre 2013 09:02
À : dev@flex.apache.org
Objet : RE: UID performance

Hi, I have created simple test suite (in FlexUnit) with  original and new method functions,
 calling 100K times createUID and the results are different for me:

-debug=true:
Old createUID : 2.652s
New createUID: 2.382s

-debug=false, optimize=true
Old createUID : 2.11s
New createUID: 1.878s

Config:  Windows 7, coreI7 , FP 11.8

Any one confirm ?

Maurice 

-----Message d'origine-----
De : Justin Mclean [mailto:justin@classsoftware.com] Envoyé : vendredi 18 octobre 2013
03:18 À : dev@flex.apache.org Objet : UID performance

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
 
     /**
      *  @private
-     *  Char codes for 0123456789ABCDEF
      */
-    private static const ALPHA_CHAR_CODES:Array = [48, 49, 50, 51, 52, 53, 54, 
-        55, 56, 57, 65, 66, 67, 68, 69, 70];
+	private static const CHAR_CODES:String = "0123456789ABCDEF";
+	
+	private static var EMPTYUID:Array = [
+		'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'
+	];
 
     //--------------------------------------------------------------------------
     //
@@ -106,53 +112,53 @@ public class UIDUtil
      *  @playerversion AIR 1.1
      *  @productversion Flex 3
      */
-    public static function createUID():String
-    {
-        var uid:Array = new Array(36);
-        var index:int = 0;
-        
-        var i:int;
-        var j:int;
-        
-        for (i = 0; i < 8; i++)
-        {
-            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
-        }
-
-        for (i = 0; i < 3; i++)
-        {
-            uid[index++] = 45; // charCode for "-"
-            
-            for (j = 0; j < 4; j++)
-            {
-                uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
-            }
-        }
-        
-        uid[index++] = 45; // charCode for "-"
-
-        var time:Number = 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 = ("0000000" + time.toString(16).toUpperCase()).substr(-8);
-        
-        for (i = 0; i < 8; i++)
-        {
-            uid[index++] = timeString.charCodeAt(i);
-        }
-        
-        for (i = 0; i < 4; i++)
-        {
-            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
-        }
-        
-        return String.fromCharCode.apply(null, uid);
-    }
+	public static function createUID():String
+	{
+		var uid:Array = EMPTYUID;
+		var index:int = 0;
+		
+		var i:int;
+		var j:int;
+		
+		for (i = 0; i < 8; i++)
+		{
+			uid[index++] = CHAR_CODES.charAt(Math.random() * 16);
+		}
+		
+		for (i = 0; i < 3; i++)
+		{
+			index++; // skip "-"
+			
+			for (j = 0; j < 4; j++)
+			{
+				uid[index++] = CHAR_CODES.charAt(Math.random() * 16);
+			}
+		}
+		
+		index++; // skip "-"
+		
+		var time:Number = 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 = ("0000000" + 
+time.toString(16).toUpperCase()).substr(-8);
+		
+		for (i = 0; i < 8; i++)
+		{
+			uid[index++] = timeString.charAt(i);
+		}
+		
+		for (i = 0; i < 4; i++)
+		{
+			uid[index++] = CHAR_CODES.charAt(Math.random() * 16);
+		}
+		
+		return uid.join("");
+	}



Mime
View raw message