in reply to Re: Compare two arrays of simple numbers
in thread Compare two arrays of simple numbers
You're right - forgot to specify that the integers are 1 to 9.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Compare two arrays of simple numbers
by graff (Chancellor) on Oct 03, 2007 at 03:01 UTC | |
I now have two identical arrays, even if the sort is ascii-betical instead of true numerical. which sort of implies that you would expect to see some integers >9 (still assuming we are only talking about positive integers). BTW, how big are these arrays, and should we expect repeated values? If there are repeated values, when you say "test if @array_1 contains exactly the same set of integers as @array_2" do you mean "the same quantities of elements for each observed value", or simply "the same values present, any number of times"? </nitpick> You've heard about the Benchmark module, right? Have you tried that with "join" vs. something else to conclude that "join" is "heavy"? As always with this sort of problem, hashes come to mind, but you would need Benchmark to see how it compares to sorting and stringifying. Here's a test of hash vs. sort-join-string-compare vs. sort-iterate-numeric-compare, checking results for both "same" and "diff" data sets (requiring that repeated values appear with the same quantity in order to be "same"), with options to change array size and max value in the array: Read more... (2 kB)
And here are the timing results (and output) for a "default" run (array size: 9, max value: 9, running on a 1GHz G4 PowerPC, macosx 10.4.10):
As you would expect, if we grow the array size but keep to the same limited number of possible array values, the "sortjoin" method will suffer more than the hash method (due to having to build and compare longer strings): But overall, I think this is an area where benchmarking, while fun and interesting, is really a bit pointless. Unless this particular task really occupies a huge proportion of what your application is supposed to do, the difference in "efficiency" among these different approaches is likely to be drowned out by everything else the app actually does (file/db/network i/o, etc). | [reply] [d/l] [select] |