2016-09-02 21 views
5

有什么办法可以比较两个日期而不用每次在python中调用strptime?我确定给我的问题没有其他办法,但要确保我已经检查了所有选项。用于比较日期的替代方案?

我正在浏览一个非常大的日志文件,每一行都有一个日期,我需要比较以查看该日期是否在两个其他日期的范围内。我不得不将每行的每个日期与导致很大瓶颈的strptime转换;

Fri Sep 2 15:12:43 2016 output2.file 

     63518075 function calls (63517618 primitive calls) in 171.409 seconds 

    Ordered by: cumulative time 
    List reduced from 571 to 10 due to restriction <10> 

    ncalls tottime percall cumtime percall filename:lineno(function) 
     1 0.003 0.003 171.410 171.410 script.py:3(<module>) 
     1 0.429 0.429 171.367 171.367 scipt.py:1074(main) 
     1 3.357 3.357 162.009 162.009 script.py:695(get_data) 
    1569898 14.088 0.000 141.175 0.000 script.py:648(check_line) 
    1569902 6.899 0.000 71.706 0.000 {built-in method strptime} 
    1569902 31.198 0.000 64.805 0.000 /usr/lib64/python2.7/_strptime.py:295(_strptime) 
    1569876 15.324 0.000 43.170 0.000 script.py:626(dict_add) 
    4709757 23.370 0.000 23.370 0.000 {method 'strftime' of 'datetime.date' objects} 
    1569904 1.655 0.000 18.799 0.000 /usr/lib64/python2.7/_strptime.py:27(_getlang) 
    1569899 2.103 0.000 17.452 0.000 script.py:592(reverse) 

日期格式化为这样;

current_date = 01/Aug/1995:23:59:53 

我正在比较他们这样;

with open(logfile) as file: 
    for line in file: 
     current_date = strptime_method(line) 
     if current_date => end_date: 
      break 

当谈到比较日期时,是否有其他选择?

编辑:谢谢大家,特别是user2539738。根据他/她的建议,结果如下:大速差;

Fri Sep 2 16:14:59 2016 output3.file 

     24270567 function calls (24270110 primitive calls) in 105.466 seconds 

    Ordered by: cumulative time 
    List reduced from 571 to 10 due to restriction <10> 

    ncalls tottime percall cumtime percall filename:lineno(function) 
     1 0.002 0.002 105.466 105.466 script.py:3(<module>) 
     1 0.487 0.487 105.433 105.433 script.py:1082(main) 
     1 3.159 3.159 95.861 95.861 script.py:702(get_data) 
    1569898 21.663 0.000 77.138 0.000 script.py:648(check_line) 
    1569876 14.979 0.000 43.408 0.000 script.py:626(dict_add) 
    4709757 23.865 0.000 23.865 0.000 {method 'strftime' of 'datetime.date' objects} 
    1569899 1.943 0.000 15.556 0.000 script.py:592(reverse) 
     1 0.000 0.000 9.078 9.078 script.py:1066(print_data) 
     1 0.021 0.021 9.044 9.044 script.py:1005(print_ip) 
     10 0.001 0.000 7.067 0.707 script.py:778(ip_api) 
+1

如果输入的日志记录按日期排序,你可能没有检查每一个日志记录在日期范围内,并且可能可以执行二分搜索以确定您的范围的开始和结束记录。只是想法。 – alecxe

+0

什么是'strptime_method'?你自己的一些代码?另外,你使用'time'(用于处理日期和时间的功能模块)还是'datetime'(基于类的模块)? –

+1

@alecxe这就是我目前已经做的。如果发现日期超出范围,它将从循环中断。但是如果你的范围很大,那么我的结果显示,这可能会很耗时,主要是因为每行被调用的方法太糟糕。 – user1165419

回答

1

我假设CURRENT_DATE是一个字符串

首先,字典

moDict = {"Aug":8, "Jan":1} #etc 

然后,找到年/月/日等

current_date = "01/Aug/1995:23:59:53" 

Yr = int(current_date[7:11]) 
Mo = moDict[(current_date[3:6])] 
Day = int(current_date[0:2]) 

m_date = datetime.datetime(Yr,Mo,Day) 

,你可以用它来比较

+0

我会毫不惊讶地发现'strptime'已经在内部完成了。你真的测试过速度吗? –

+0

@DavidHeyman检查我的结果 – user1165419

+0

@DavidHeyman即使'strptime'在内部执行此操作,它也必须解释格式字符串。另一方面,它不需要解释Python。 :) – Kaz

1

由于您的日期显示为固定长度格式,因此分析起来非常简单,您不需要strptime即可完成此操作。您可以将它们重新排列到ISO 8601 date/time format中,并将它们直接作为字符串进行比较!

mos = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'} 

def custom_to_8601(dt): 
    return dt[7:11] + '-' + mos[dt[3:6]] + '-' + dt[0:2] + 'T' + dt[12:] 

>>> custom_to_8601('01/Aug/1995:23:59:53') 
'1995-08-01T23:59:53' 

这可能是一个触摸更快地使用join而不是字符串连接,并离开了标点符号:

def comparable_date(dt): 
    return ''.join([dt[7:11], mos[dt[3:6]], dt[0:2], dt[12:]]) 

>>> comparable_date('01/Aug/1995:23:59:53') 
'1995080123:59:53' 

运行cProfile上百万重复对我产生这些时间:

  • custom_to_8601:0.978秒
  • comparable_date:0.937秒
  • 你原来的代码strptime:15.492秒
  • 使用datetime构造较早的答案:1.134秒
+0

谢谢,我要试试这个,并在我的结果中回复你! – user1165419