You could try to do this with multiprocessing, but that's not going to help you much as it will only cut the time to compute the answer in half. You said it takes forever, and forever / 2 is still forever. You need a different algorithm. Try sets
set1 = set(l1)
set2 = set(l2)
new_ids = list(set2 - set1)
erased_ids = list(set1 - set2)
Your algorithm runs in O(n^2). This is because [x for x in l2 if x not in l1]
checks all of l1 for an x, for every x in l2. If l1 and l2 have 8m elements, that requires 8000000^2 = 160000000000000 checks.
Instead, a set is a data structure (which uses hashing internally) that can check for element membership in one operation, or O(1). In theory, checking if a number x
is in a set takes the same amount of time whether the set has one element or 8 million. This is not true for a list.
Sets can also be subtracted. set2 - set1
means "the things in set2 that aren't in set1". This is done in O(n) time, I presume by doing n O(1) checks for membership.
Building the sets in the first place is also O(n) time, as adding to a set is an O(1) operation and you must do it to n elements.
Therefore, this whole algorithm runs in O(n) time.