python - Difference in run time complexity of two seemingly similar solutions
问题描述
I was trying to solve an assignment and wrote a solution that seems very close to the solution I found online when I set out to find more efficient solutions.
This is the assignment statement
The goal of this problem is to implement a variant of the 2-SUM algorithm covered in this week's lectures.
The file contains 1 million integers, both positive and negative (there might be some repetitions!).This is your array of integers, with the ith row of the file specifying the ith entry of the array.
Your task is to compute the number of target values t in the interval [-10000,10000] (inclusive) such that there are distinct numbers x,y in the input file that satisfy x+y=t The variable
all_ints
is a list containing all the integers
hashtable = set(all_ints)
min_t = -1024
max_t = -min_t + 1
solutions = 0
k = 0
from time import time
# Solution 1 that I wrote
t0 = time()
for n in range(min_t, max_t):
solutions+=1 if any([True if 2*i!=n and n-i in hashtable else False for i in hashtable]) else 0
# Solution 2 that I found online
t1 = time()
solutions2 = sum(1 for n in range(min_t, max_t) if any(n - x in hashtable and 2 * x != n for x in hashtable))
t2 = time()
print(t1-t0) #857.0168697834015
print(t2-t1) #591.8803908824921
Upon basic inspection, these two solutions look very similar. Yet, their run times are quite different and while both scale linearly, they deviate further away when I decrease the value of min_t
.
What is the underlying difference between the two implementations that causing this
解决方案
您的额外列表理解创建可能会减慢您的算法,从而提供额外的容器开销。
第二种解决方案使用生成器,它在计算时立即产生值,而不需要列表。
推荐阅读
- xml - 我可以将我的 android 应用程序 xml 文件转换为 adobe xD 文件吗?
- java - 检查 JTextField 的内容,然后存储在 String 以从另一个类访问
- javascript - 以特定方式对饼图图例进行 Echart 配置
- rollup - rollup,如何同时捆绑 .css 和 min.css
- javascript - 当我使用 javascript 添加时,图像不显示
- python - 熊猫:将列转换为 timedelta64
- laravel - 此路由不支持 PATCH 方法。支持的方法:GET、HEAD
- typescript - 使用 typescript 解析 webpack/metro mainFiles 模块
- php - API 未获取访问令牌
- xml - xpath - 显示多行节点文本的特定行(最后一行)