1 package {
2 import flash.utils.ByteArray;
3 import flash.errors.EOFError;
4 import flash.display.Sprite;
5 import flash.net.LocalConnection;
6 import flash.utils.getTimer;
7
8 import flash.events.*;
9 import flash.display.*
10
11 import flash.text.TextField;
12 public class ByteArrayTest extends Sprite {
13 private var Test_AMOUNT:int=1000000;//执行次数
14 private var testStr:String;//测试字符串
15 private var testByteArr:ByteArray;//测试二进制字符串
16 private var strByteArr:ByteArray;
17
18 public var btn_1:DisplayObject;
19 public var btn_2:DisplayObject;
20 public var times_txt:TextField;
21 public var totalTime_txt:TextField;
22
23 private var testArr:Array;
24
25
26
27 public function ByteArrayTest() {
28 testStr="00012,13,5,7";
29 testByteArr = new ByteArray();
30 testByteArr.writeMultiByte("00012", "gb2312");
31 testByteArr.writeMultiByte("13", "gb2312");
32 testByteArr.writeInt(5);
33 testByteArr.writeInt(7);
34 //teatByteArr.writeByte(0)
35 strByteArr = new ByteArray();
36 strByteArr.writeMultiByte(testStr, "gb2312");
37
38 btn_1.addEventListener(MouseEvent.CLICK,click1);
39 btn_2.addEventListener(MouseEvent.CLICK,click2);
40
41 }
42 private function click1(Event:MouseEvent):void {
43 initStr();
44 Test_AMOUNT = int(times_txt.text);
45 }
46 private function click2(Event:MouseEvent):void {
47 initByteArr();
48 Test_AMOUNT = int(times_txt.text);
49 }
50 //初始化 通过通常手段 显示
51 private function initStr():void {
52
53 var t:Number = getTimer();
54 for (var i:int=0; i < Test_AMOUNT; i++) {
55 strByteArr.position=0;
56 var temp:String = strByteArr.readMultiByte(12,"gb2312");
57 testArr = temp.split(",");
58 testArr[0] = String(testArr[0]);
59 testArr[1] = String(testArr[1]);
60 testArr[2] = uint(testArr[2]);
61 testArr[3] = uint(testArr[3]);
62 }
63 trace(testArr);
64 trace("使用String总共耗时:"+Number(getTimer()-t)+"毫秒");
65 totalTime_txt.text =String(Number(getTimer()-t))
66 }
67 //初始化 通过BitmapData快照 显示
68 private function initByteArr():void {
69 var t:Number = getTimer();
70 testArr = new Array;
71 for (var i:int=0; i < Test_AMOUNT; i++) {
72 //if (testByteArr.position != 0) {
73 testByteArr.position= 0;
74 //}
75 testArr[0] = testByteArr.readMultiByte(5, "gb2312");
76 testArr[1] = testByteArr.readMultiByte(2, "gb2312");
77 testArr[2] = testByteArr.readInt();
78 testArr[3] = testByteArr.readInt();
79 }
80 trace(testArr);
81 trace("使用ByteArr总共耗时:"+Number(getTimer()-t)+"毫秒");
82 totalTime_txt.text = String(Number(getTimer()-t))
83 }
84
85
86 }
87 }
88 //结果如下:100000次
89
90 //00012,13,5,7
91 //使用String总共耗时:10736毫秒
92
93 //00012,13,5,7
94 //使用ByteArr总共耗时:6312毫秒
2 import flash.utils.ByteArray;
3 import flash.errors.EOFError;
4 import flash.display.Sprite;
5 import flash.net.LocalConnection;
6 import flash.utils.getTimer;
7
8 import flash.events.*;
9 import flash.display.*
10
11 import flash.text.TextField;
12 public class ByteArrayTest extends Sprite {
13 private var Test_AMOUNT:int=1000000;//执行次数
14 private var testStr:String;//测试字符串
15 private var testByteArr:ByteArray;//测试二进制字符串
16 private var strByteArr:ByteArray;
17
18 public var btn_1:DisplayObject;
19 public var btn_2:DisplayObject;
20 public var times_txt:TextField;
21 public var totalTime_txt:TextField;
22
23 private var testArr:Array;
24
25
26
27 public function ByteArrayTest() {
28 testStr="00012,13,5,7";
29 testByteArr = new ByteArray();
30 testByteArr.writeMultiByte("00012", "gb2312");
31 testByteArr.writeMultiByte("13", "gb2312");
32 testByteArr.writeInt(5);
33 testByteArr.writeInt(7);
34 //teatByteArr.writeByte(0)
35 strByteArr = new ByteArray();
36 strByteArr.writeMultiByte(testStr, "gb2312");
37
38 btn_1.addEventListener(MouseEvent.CLICK,click1);
39 btn_2.addEventListener(MouseEvent.CLICK,click2);
40
41 }
42 private function click1(Event:MouseEvent):void {
43 initStr();
44 Test_AMOUNT = int(times_txt.text);
45 }
46 private function click2(Event:MouseEvent):void {
47 initByteArr();
48 Test_AMOUNT = int(times_txt.text);
49 }
50 //初始化 通过通常手段 显示
51 private function initStr():void {
52
53 var t:Number = getTimer();
54 for (var i:int=0; i < Test_AMOUNT; i++) {
55 strByteArr.position=0;
56 var temp:String = strByteArr.readMultiByte(12,"gb2312");
57 testArr = temp.split(",");
58 testArr[0] = String(testArr[0]);
59 testArr[1] = String(testArr[1]);
60 testArr[2] = uint(testArr[2]);
61 testArr[3] = uint(testArr[3]);
62 }
63 trace(testArr);
64 trace("使用String总共耗时:"+Number(getTimer()-t)+"毫秒");
65 totalTime_txt.text =String(Number(getTimer()-t))
66 }
67 //初始化 通过BitmapData快照 显示
68 private function initByteArr():void {
69 var t:Number = getTimer();
70 testArr = new Array;
71 for (var i:int=0; i < Test_AMOUNT; i++) {
72 //if (testByteArr.position != 0) {
73 testByteArr.position= 0;
74 //}
75 testArr[0] = testByteArr.readMultiByte(5, "gb2312");
76 testArr[1] = testByteArr.readMultiByte(2, "gb2312");
77 testArr[2] = testByteArr.readInt();
78 testArr[3] = testByteArr.readInt();
79 }
80 trace(testArr);
81 trace("使用ByteArr总共耗时:"+Number(getTimer()-t)+"毫秒");
82 totalTime_txt.text = String(Number(getTimer()-t))
83 }
84
85
86 }
87 }
88 //结果如下:100000次
89
90 //00012,13,5,7
91 //使用String总共耗时:10736毫秒
92
93 //00012,13,5,7
94 //使用ByteArr总共耗时:6312毫秒