-- Zoom.Quiet [2004-08-11 16:44:45]

# 有效的处理选择的结构数据对

2.3 Processing Selected Pairs of Structured Data Efficiently 有效的处理选择的结构数据对

Credit: Alex Martelli, David Ascher

## 问题 Problem

You need to efficiently process pairs of data from two large and related data sets.

## 解决 Solution

Use an auxiliary dictionary to do preprocessing of the data, thereby reducing the need for iteration over mostly irrelevant data. For instance, if xs and ys are the two data sets, with matching keys as the first item in each entry, so that x[0] == y[0] defines an "interesting" pair:

```   1 auxdict = {}
2 for y in ys: auxdict.setdefault(y[0], []).append(y)
3 result = [ process(x, y) for x in xs for y in auxdict[x[0]] ]
```

## 讨论 Discussion

To make the problem more concrete, let's look at an example. Say you need to analyze data about visitors to a web site who have purchased something online. This means you need to perform some computation based on data from two log files梠ne from the web server and one from the credit-card processing framework. Each log file is huge, but only a small number of the web server log entries correspond to credit-card log entries. Let's assume that cclog is a sequence of records, one for each credit-card transaction, and that weblog is a sequence of records describing each web site hit. Let's further assume that each record uses the attribute ipaddress to refer to the IP address involved in each event. In this case, a reasonable first approach would be to do something like:

```   1 results = [ process(webhit, ccinfo) for webhit in weblog for ccinfo in cclog \
```

The problem with this approach is that the nested list comprehension will iterate over each entry in the web server log, and, for each entry in the web server log, it will also iterate over each entry in the credit-card log. This means that the algorithm has O(M x N) performance characteristics梚n other words, the time it takes to compute will be proportional to the product of the size of both logs. As the web site becomes more popular and as data accumulates, the performance of the algorithm will rapidly deteriorate.

The key to optimizing this algorithm is to recognize that the computation (process) needs to happen for only a small subset of all of the possible combinations of the two variables (in this case, webhit and ccinfo). If we could search for only the right pairs, we might be able to speed up the process. As Tim Peters says in the introduction to this chapter, if you need to search, use an auxiliary dictionary. The solution described earlier, rewritten to use our variables, yields:

```   1 ipdict = {}
2 for webhit in weblog: ipdict.setdefault(webhit.ipaddress, []).append(webhit)
3 results = [ process(webhit, ccinfo) for ccinfo in cclog \
4                                     for webhit in ipdict[ccinfo.ipaddress] ]
```

The highlighted line creates a dictionary mapping IP addresses to lists containing the data for each web hit. Because we're indexing the dictionary by IP address, we are optimizing the data structures for a particular query: "give me all of the web records for a particular IP address." The list comprehension now iterates over only the data we require梖or each credit-card transaction, we iterate over all of the web hits corresponding to the IP address used in that transaction. Not only did the algorithm go from O(M x N) to O(M+N) from a theoretical point of view, but, because we chose to hold in the auxiliary dictionary data that is sparser from the point of view of the task at hand, we've made the solution faster than the alternative (which would also be O(M+N)).

Note that the test used to determine whether two records correspond to a pair of interest can be arbitrary. The generic description of the solution uses indexing, while the web example uses attribute-getting. The test you use will depend on your application and your data structures.

PyCkBk-1-16--Recipe 1.6 for more details on the setdefault method of dictionaries; the introduction to Chapter 2.

PyCkBk-2-3 (last edited 2009-12-25 07:14:03 by localhost)

• Page.execute = 1.238s
• getACL = 0.180s
• i18n_init = 0.191s
• init = 0.207s
• loadLanguage = 0.003s
• load_multi_cfg = 0.137s
• run = 5.503s
• send_page = 5.221s
• send_page_content = 1.263s
• send_page_content|1 = 0.322s
• send_page|1 = 0.449s
• total = 5.710s