Hi!
You write that you use nested for. That is a very inefficient way to do it. Suppose n is the size of the array,
you would need O(n^2) compares. In your case it is ~7000000*7000000, which is very much.
The following algorithm would be much faster O(n) (or ~7000000*3):
- create a temporary hash
- add all elements of the first array to that hash; use the array-value as a key and 1 as a value
- pass through the second array; if the current value is already in the hash, increase its value by 2
- pass through the hash; if all values are 3, array1 is part of array2
Another idea could be to sort both arrays (the perl sort-function is more efficient than a "nested-for-(bubble-)sort")
and then work on the sorted arrays.
HTH, Rata
PS.: please be aware that the algorithm above does not work if the first array contains some values more than once!
PPS.: and of course you can improve the algorithm by adding checks on "more than once" in step 3 - and aborting the rest of the loops then
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.