A small programming challenge

Alex Tweedly alex at tweedly.net
Sun Apr 14 17:58:13 EDT 2013


Yes, a few interesting aspects.

I too was surprised that the (intuitive) array method was slower than 
the string-based method, so I looked into it some more.

The string method took 35-37 msec on my machine.
The array method took 84-85 msecs.

But i realized that as well as changin string -> array, you had 
effectively changed from column-first to row-first processing, and 
row-first means you have a larger initial time before you can get a 
duplicate. I changed the array method to be column-first, which 
decreased the time to 54-56 msecs -- still much slower.

But of course, if you do it in column order, there is no need to use an 
array, so I switched to s scalar variable, and decreased the time to 
29-32 msecs; roughly 15% improvement.  Thereby showing that "contains" 
is marginally faster than "replace ... with empty". Hmmm - no real 
surprise :-(

Other observation is that in the "optimized" version, we already know 
there are no duplicates in the existing lines - only the last line can 
possibly duplicate with an earlier one. So you can modify "checkit" to 
use that fact, and get the time down to 24-25 msecs.

Let's see that gave me twenty minutes of fun coding to save 12 
millisecs. Just as well I enjoyed it, because the time saving didn't 
justify the effort.

-- Alex.


On 12/04/2013 16:34, Geoff Canyon wrote:
> http://gcanyon.wordpress.com/2013/04/12/brute-force-insanity/
>
> I posted my answer, so don't scroll down if you want to try yourself. There
> were some interesting aspects to the code.
>
> gc
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list