Tag Archive for 'Performance'

100x times faster MD5 and more

Update: Get more recent version of the library here.

Update: I updated the zip with LGPL MIT license file. The original author basically says “use wherever you want” which looks like mostly LGPL MIT, right? Yes, haXe was used to create the SWC but I don’t know if we can get the source or not. Anyway I hope you understand from the code below how to use the library.

My dear friend BlooDHounD has recently released an SWC with highly optimized MD5, Base64, CRC32, JPEG, PNG algorithms. Compared to as3corelib we get the following results:

by.blooddy.crypto.MD5.hashBytes: 40
com.adobe.crypto.MD5.hashBytes: 4483

by.blooddy.crypto.Base64.encode: 115
mx.utils.Base64Encoder: 1635

by.blooddy.crypto.Base64.decode: 141
mx.utils.Base64Decoder: 2762

by.blooddy.crypto.image.JPEGEncoder.encode: 447
com.adobe.images.JPGEncoder: 3496

by.blooddy.crypto.image.PNG24Encoder.encode: 538
com.adobe.images.PNGEncoder.encode: 1423

Here’s the benchmark source:

package {

	import by.blooddy.crypto.Base64;
	import by.blooddy.crypto.MD5;
	import by.blooddy.crypto.image.JPEGEncoder;
	import by.blooddy.crypto.image.PNG24Encoder;

	import com.adobe.crypto.MD5;
	import com.adobe.images.JPGEncoder;
	import com.adobe.images.PNGEncoder;

	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;

	import mx.utils.Base64Decoder;
	import mx.utils.Base64Encoder;

	[SWF( scriptTimeLimit="255" )]
	public class test extends Sprite {

		/**
		 * Constructor
		 */
		public function test() {
			super();

			var text:String = '';
			var t:Number;

			// BitmapData for tests
			var bmp:BitmapData = new BitmapData( 1024, 1024, true, 0xFFFF0000 );
			bmp.noise( int( Math.random() * int.MAX_VALUE ), 0, 0xFF, 7, false ); // генерируем шум

			// ByteArray for tests
			var bytes:ByteArray = bmp.getPixels( bmp.rect );

			// MD5 test
			t = getTimer();
			by.blooddy.crypto.MD5.hashBytes( bytes );
			text += '\nby.blooddy.crypto.MD5.hashBytes: ' + ( getTimer() - t ); // 40

			t = getTimer();
			com.adobe.crypto.MD5.hashBytes( bytes );
			text += '\ncom.adobe.crypto.MD5.hashBytes: ' + ( getTimer() - t ); // 4483

			// Base64 encode test
			t = getTimer();
			var s1:String = by.blooddy.crypto.Base64.encode( bytes, true );
			text += '\nby.blooddy.crypto.Base64.encode: ' + ( getTimer() - t ); // 115

			t = getTimer();
			var e:Base64Encoder = new Base64Encoder();
			e.encodeBytes( bytes );
			var s2:String = e.flush();
			text += '\nmx.utils.Base64Encoder: ' + ( getTimer() - t ); // 1635

			// Base64 decode test
			t = getTimer();
			by.blooddy.crypto.Base64.decode( s1 );
			text += '\nby.blooddy.crypto.Base64.decode: ' + ( getTimer() - t ); // 141

			t = getTimer();
			var d:Base64Decoder = new Base64Decoder();
			d.decode( s2 );
			d.flush();
			text += '\nmx.utils.Base64Decoder: ' + ( getTimer() - t ); // 2762

			// JPEG test
			t = getTimer();
			by.blooddy.crypto.image.JPEGEncoder.encode( bmp, 60 );
			text += '\nby.blooddy.crypto.image.JPEGEncoder.encode: ' + ( getTimer() - t ); // 447

			t = getTimer();
			( new com.adobe.images.JPGEncoder( 60 ) ).encode( bmp );
			text += '\ncom.adobe.images.JPGEncoder: ' + ( getTimer() - t ); // 3496

			// PNG test
			t = getTimer();
			by.blooddy.crypto.image.PNG24Encoder.encode( bmp );
			text += '\nby.blooddy.crypto.image.PNG24Encoder.encode: ' + ( getTimer() - t ); // 538

			t = getTimer();
			com.adobe.images.PNGEncoder.encode( bmp );
			text += '\ncom.adobe.images.PNGEncoder.encode: ' + ( getTimer() - t ); // 1423

			var tf:TextField = new TextField();
			tf.autoSize = 'left';
			tf.text = text;
			super.addChild( tf );

		}

	}

}

I know that he is too shy to tell you guys about his results (8
But they are definitely worth sharing.

You can download the SWC here (look up at the update section).

OMG! as3-signals are faster than events!!!!!1

Continuing on as3-signals. Here the guy actually ran performance tests on as3-signals vs. events. And got surprisingly SHOCKING results. You won’t gonna believe — AS3-SIGNALS ARE FASTER THAN EVENTS!!!!!1 Who could have thought that, huh? OK, </sarcasm>. Just looking at the code you can tell that as3-signals being simpler will be faster. This is definitely good.

But, if you need 0(zero) or 1(one) listener for your object, use plain old callbacks and you will be shocked by performance boost!

I added this code to the guy’s test:

public var callback:Function;
public function dataCallback():void
{
for (var i:uint = 0; i < loops; i++)
this.callback(i);
}

And here are the results

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
1 (5 iterations)
Player version: MAC 10,0,42,34 (debug)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
Events                                                     3107   621.40
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
1 (5 iterations)
Player version: MAC 10,0,42,34 (debug)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
Signals                                                    2198   439.60
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
1 (5 iterations)
Player version: MAC 10,0,42,34 (debug)
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
Callback                                                    255    51.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Someone has too much time running tests and drawing graphs. Oh wait, it’s me. At least I haven’t drawn a graph.

P.S. Once again, as3-signals is good, opensource is good! cheers~