简体   繁体   English

优化速度条件

[英]optimizing conditions for speed

example code: 示例代码:

e = [[1,2,3,4],[0,...,10000],[4,5,6,7]]
for n in range(0,1000000):
  if n in e[0] and n in e[1] and n in e[2]:
    yield n

I'm trying to optimize for speed to avoid e[1] lookup for every iteration if i can find out before that e[1] == range. 如果我能在e [1] == range之前找到,我会尝试优化速度以避免每次迭代的e [1]查找。 Same goes for e[0] == range or e[2] == range e [0] ==范围或e [2] ==范围也一样

let me clarify this a bit more..im trying to write crontab http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5 parser in python. 让我澄清一下。.im试图用python编写crontab http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5解析器。

Format for crontab is * * * * * crontab的格式为* * * * *

where first field is minutes range(0,60), second hours range(0,23), third days of month range(1,32), 4th month range(1,13), 5th day of week range(0,8) 其中第一个字段是分钟范围(0,60),第二小时范围(0,23),每月第3天范围(1,32),第4个月范围(1,13),每周第5天范围(0,8) )

when every field is matched something happens, ie for every day at 5,30 am format is 当每个字段都匹配时,即发生某事,即每天凌晨5,30,格式为

30 5 * * * 30 5 * * *

now simplest solution is to iterate through lower,upper boundary with increment of 60 seconds, since smallest increment is 1 minute. 现在最简单的解决方案是以60秒的增量迭代上下边界,因为最小增量为1分钟。 and replace * with full range, matched fields with single number enclosed in list. 并将*替换为完整范围的匹配字段,并在列表中加上单个数字。

simplest algorithm is 最简单的算法是

for ts in range(startts,endts,60): 
  now = time.localtime(ts)
  if now.tm_min in [5,] and now.tm_hour in [30,] and now.tm_mon in range(0,13) ... etc:
    yield now

since some of those fields are sometimes * list lookup is not needed, and im trying to figure out way to dynamically generate conditions based on '*' 由于某些字段有时是*不需要列表查找,因此我试图找出基于'*'动态生成条件的方法

Looks like you want to find all elements that are common to all three lists. 看来您想查找所有三个列表共有的所有元素。 This is essentially the same as finding the intersection of the sets represented by the lists. 这本质上与找到列表表示的集合的交集相同。 In Python we can simply use set and find the intersection. 在Python中,我们可以简单地使用set并找到交集。

#! /usr/bin/env python

def get_values(lst):
    sets=map(set,lst)
    common=reduce(lambda x,y:x.intersection(y),sets,set(lst[0]))
    for value in common:
        yield value
if __name__=='__main__':
    e = [[1,2,3,4],range(10000),[4,5,6,7]]  
    for common_item in get_values(e):
        print common_item
if n==4:

will do it. 会做的。 At least in your example. 至少在您的示例中。

Perhaps you should tell us what you're actually trying to do? 也许您应该告诉我们您实际上想做什么? Are the ranges always contiguous? 范围是否总是连续的? What exactly are the constraints you're working under? 您所受的约束到底是什么?

I think this should work: 我认为这应该工作:

e = [[1,2,3,4],[0,...,10000],[4,5,6,7]]
for n in e[0]:
  if and n in e[2] and n in e[1]:
    yield n

It will only check the values which exist in e[0]. 它只会检查e [0]中存在的值。 If the numbers of elements are different in each sub-list, you could add one further generic optimisation which checks them in order of the size of lists so you check against the smallest list first (I did that by hand in this case, but you get the idea). 如果每个子列表中元素的数量不同,则可以添加进一步的通用优化,该优化以列表大小的顺序对其进行检查,因此您首先要对照最小的列表进行检查(在这种情况下,我是手工完成的,但是知道)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM