on Sat, 31 Aug 2002 02:23:20 GMT, [EMAIL PROTECTED] (Sudarshan Raghavan) wrote:
> On 30 Aug 2002, Felix Geerinckx wrote: > >> There is no need to sort the full array (as others suggested) to >> just find the lowest and highest number when an O(n) operation >> will suffice: > > Precisely the comment that I had a few months back for a similar > question. Read through this thread > http://archive.develooper.com/beginners%40perl.org/msg22716.html Thanks for this reference (from a time before I discovered the perl beginners list). > Benchmark: timing 1000000 iterations of using_for, using_sort... > using_for: 11 wallclock secs (11.60 usr + 0.04 sys = 11.64 CPU) > @ 85910.65/s (n=1000000) > using_sort: 8 wallclock secs ( 7.63 usr + 0.06 sys = 7.69 CPU) > @ 130039.01/s (n=1000000) This if for a seven-element array. I did some testing myself for larger array sizes (times are in CPU seconds): Size Loop Sort 1000 0.00 0.01 10000 0.02 0.08 100000 0.13 1.03 200000 0.24 2.18 300000 0.37 3.35 So if you have more than 1000 numbers, the loop method will already be faster. In addition, it has the advantage that you don't need all the numbers to be present in memory, since you can calculate min/max while you read the numbers from file. For small array sizes, it doesn't matter which method you use, since the cpu time will be near zero anyway. For larger sizes it does matter, and the loop method is a clear winner. Therefor, imho, your suggestion in March (and mine now :-) to use a loop, is *always* preferable. -- felix -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]