首页 > Python 处理文件的性能优化

Python 处理文件的性能优化

现在有一个list包含有1500个topic,另外一个文件包含一亿个微博数据,现在我想统计,1500个topic中每个topic分别有多少条微博包含它们,我写的代码如下,但是运行起来需要非常久的时间,有什么办法可以优化吗?

    f = file("largefile")
    for line in f:
        try:
            tweet_time = line.split(',',3)[2].split()[0]  # 微博发布时间
            tweet = line.split(',',3)[-1]  # 微博内容
            for topic in topics:
                topic_items = topic.split()  # 每个topic可能有多个词组成
                isContain = True
                for item in topic_items:
                    if item not in tweet:
                        isContain = False
                        break
                if isContain:
                    pass   # 该微博包含该topic
        except:
            continue
    f.close()

  1. 分块
  2. 并行处理
  3. 加总

参见:

  1. http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html
  2. http://en.wikipedia.org/wiki/MapReduce
【热门文章】
【热门文章】