November 2003 Archives

Finally Finished!

| 1 Comment

I just finished up an article for the ColdFusion Developer's Journal. It's on integrating Java in your Coldfusion applications and should be published in the January issue. I don't think I can preview any of it here.. so just pick up the magazine in January if you're interested.

How was everyone's Thanksgiving break? I've spent most of mine working -- I've been pretty busy with finishing up the aforementioned article and with preparing my slides for MX Down Under. Jesse and I have also been collaborating on a Central app that is going to knock everyone's socks off when we release it. So that, combined with the holiday and spending time with family, means I haven't had a lot of time to post anything new.

Oh, have you checked out the new SuSE Linux 9.0? It was released for FTP install last Monday. Just download the boot.iso, and you can do an free FTP install instead of buying it (high speed connection is almost required though). I have it set up on one of my boxes and I'm really enjoying using it. If it weren't for needing Macromedia Products, I'd be using Linux exclusively...

Things should be getting back to normal this week since I have a few projects wrapping up. That'll leave me a little more time to tend to this blog and post some quality entries. I also have a new design in the works that I just haven't had time to implement, but I hope to get that finished this week. I've been using this default movable type style since I first started the blog... yikes!

I hope everyone had a great Thanksgiving holiday!

ActionScript 2: Why set calls get

| 5 Comments

For those of you that may not be aware... if you use properties in your ActionScript 2 classes there is an implicit call to the get method whenever set is invoked. I believe Peter Hall was the first to point this out, and Robin Debreuil mentioned it on FlashCoders as well. I wanted to give this behavior a little more publicity, so I'm bringing it up again. Near the end of this entry, I also offer a way to avoid this behavior by manipulating some bytecode.

This behavior can be a bit of a headache if you don't realize that it is happening behind the scenes, especially if you're doing something in your get function other than simply returning a value.... which leads us to wonder why this even happens in the first place. If I set a property, why does the get method fire at all? In other languages that implement properties (C#, for example), there is no implicit get invocation, so it appears that this is just another "Flash quirk." We'll need to dig into the bytecode to figure out what Flash is actually doing...

Here we go! (Read on)

SWT Flash

| 6 Comments

This is kind of old news, but I haven't really seen much about this on the internet. Every example out there demonstrating Flash in a desktop application is usually done in Visual Basic or C#. Thanks to SWT Flash, you can somewhat easily embed Flash into Java applications as well. Sadly, this is a Windows-only solution for now(?), and the Flash plug-in must already be installed.

SWT Flash hasn't been updated since 7/30/2002. It still works though, and it's a great way to integrate Flash into your Java applications. Below is some example code to help you out. You'll need Eclipse, SWT (found under "SWT Binary and Source" on the Eclipse downloads page after selecting a site and then a release) and SWT Flash. You'll also need the Flash plug-in installed, but if you're here I'm assuming you've already got that step taken care of.

Much faster array sorting

| 3 Comments

Need to sort an Array? No sweat, Array's have a built-in "sort" method. You decide to call my_array.sort() only to find out your numbers aren't sorted correctly. You build a custom comparison function to pass into sort only to realize that sorting 3000 numbers takes 261 milliseconds. What the heck?

We all know that the built-in Array sort method isn't the fastest around. The best way around the speed issue is to either build your own sorting method, or use someone else's. Prototype is a good place to start looking for code, and now you've brought that 261 milliseconds down to 127 with the qSort method, and even further down to 103 milliseconds with sortPInt. Still, I can't help but think that we can do better...

So.. thinking a bit about the problem, I've come up with my own custom sort method. At it's heart it resembles the "Bin Sort" algorithm. I was able to reduce the sorting time down to 58 milliseconds (from the original 261). Sweetness.

For starters, this sort method was built for a project I'm working on and thus only functions correctly under a given set of conditions. There are really only 2 conditions that need to be met for this sort to work.

  1. All of the array elements must be integers (no decimals!) - positive and negative integers will both work
  2. The theoretical min and max values of the elements inside the array must be known

If those conditions aren't met, then don't expect correct sorting results using my custom sort method. It's ok if the values you supply aren't the actual min and max values contained in the array - they only need to be the theoretical min and mix. But, the closer they are to the actual values the better performance you will see.

To start off, here is the code I'm using to test. What this does is generate an array of 1000 integers, ranging from -1005 to 1994. Where did these numbers come from? Why, I made them up for this example of course!

range = 3000;
min = -1005;
max = min + range - 1 // 1994

// create the numbers array and populate it
numbers = new Array(1000);
for (var i = 0; i < 1000; i++) {
	numbers[i] = Math.floor(Math.random() * range) + min;	
}

Note that declaring the array a fixed size and adding elements with the [] operator is faster (in this case, at least) than declaring a dynamic array (numbers = new Array()) and adding elements with "push" (numbers.push(newElement)). How much faster? Well, the dynamic array takes my computer around 51 milliseconds to build, whereas the fixed-length array takes only 45. This isn't a huge difference, but sometimes every little bit counts. Plus - Since we know we're going to have 1000 elements, why would we make the array dynamic anyway? That's a rhetorical question, for the most part.

Next comes testing the built in sort method. Here we go:

// assuming numbers is built like the code snippet above
start_t = getTimer();
numbers.sort();
trace("time: " + (getTimer() - start_t) + " ms");
trace(numbers);  

With the above code snippet, you can see that the numbers aren't sorted correctly. Flash thinks that we're trying to sort strings instead of numbers (for example, 1403 comes before 304 because 1 is less than 3). So, we build a custom comparison function and run the test again:

function numCompare(a, b) {
	if (a < b) return -1
	if (a == b) return 0;
	return 1;
}

start_t = getTimer();
numbers.sort(numCompare);
trace("time: " + (getTimer() - start_t) + " ms");
trace(numbers);

That's better... but by golly that beast is slow. At least we got correct results this time, but let's see what we can do to improve the sort time.

I repeated the same testing process for those 2 prototypes mentioned in the opening paragraph....

Copy the Array.prototype.qSort method to the frame replace "numbers.sort(numCompare)" with "numbers.qSort(0, numbers.length-1)" - this is faster, but not fast enough.

Trying again.. copy the sortPInt method to the frame, and change the sorting line to "numbers.sortPInt()" - again, a little bit faster, but still not quite fast enough. Note that this sort only takes positive integers, so you'll need to change the min value to a positive integer for the sort to work correctly.

Finally, get fed up and write your own custom sort method. Copy the code below, and then replace the sort call with "numbers.customSort(min, max)". Min is the smallest possible value in the array, and max is the largest possible value in the array. Note that these variables were defined right about the for loop that populates the numbers array. Also note that these are the theoretical min and max.. and may not actually be the min and max elements contained in the array.

// Darron Schall
// 11/07/03
// pre: min and max are the theoretical min and max values
//     contained in the array.  the array contains only integers.
// post: the array will be sorted in ascending order
Array.prototype.customSort = function(min, max) {
	var i;
	var bins = new Array(max-min+1);
	var binvalue;
	
	// distribute array to bins
	i = this.length;
	while (i--) {
		binvalue = this[i]-min;
		if (bins[binvalue] == undefined) {
			bins[binvalue] = 0;
		}
		bins[binvalue]++;
	} 
	
	// rebuild - this may see some speed gains
        // by making the array fixed-length instead
        // of dynamic
	var tmp = new Array();
	i = bins.length;
	while (i--) {
		if (bins[i] != undefined) {
			while (bins[i]--) {
				tmp.push(i);
			}
		}
	}
	
	// reconstruct this from tmp, reversing the order
        i = this.length;
	while(i--) {
		this[this.length-i-1] = tmp[i]+min;
	}
}

As stated before, my customSort method takes only 58 milliseconds to execute on my computer. Considering the built-in method takes 261 milliseconds, I would call this sort algorithm a success in this scenario. Of course, my custom sort may not always be the fastest depending on the number of elements in the Array. It seems to really outperform when the integer array to sort is large.

Read on for an explanation of my algorithm...

MovableType problems

| No Comments

Wow, I've been having a lot of issues with my site recently. My comments broke on me (Jesse had similar issues at one point), and I was no longer able to log in to post any more entries.

In order to get my site working, I had to re-install MovableType from scratch. This included deleting my previous database -- so I had to re-enter all of my posts (copy/paste to the rescue!). It was a tedious process and took me a few hours to complete... The good side is that I recovered every post successfully. The bad side is that every single one of my comments has been lost! Doh!

So... it looks like I'm good to go again. The moral of the story is to make sure you always backup your data! I'll be doing weekly backups from this point out, just in case.

And.. it looks like the problem was directly related to my host switching configurations on me (moving from Windows 2k to Windows 2k3), and in the process upgraded versions of "DB_File.pm." Because of the upgrade, my old database became unusable. I found out only too late that there is a process you can go through to upgrade the database as well... I wish I would've found this link before I already deleted everything and started re-entering the information.

Either way... I'm back. Expect something cool tomorrow!

FMX2k4: Rendering html in a ListBox

| 28 Comments

I'm currently in the middle of a project that uses the new ListBox component in Flash MX 2004. So far things were going good, until I wanted to display part of the list entries in bold. I thought it might be as simple as changing the label field when adding items to include the html tags, but of course life usually isn't that easy. However, after a little reading and digging around I eventually came up with a solution that I'm sharing with you today.

Below is the end result, and you can re-create this in just a few easy steps.





About this Archive

This page is an archive of entries from November 2003 listed from newest to oldest.

October 2003 is the previous archive.

December 2003 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Archives

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.02