Skip to content

Instantly share code, notes, and snippets.

@Snawoot
Created May 22, 2019 20:52
Show Gist options
  • Save Snawoot/12c70421d5fdea84bab7d0698999c7b4 to your computer and use it in GitHub Desktop.
Save Snawoot/12c70421d5fdea84bab7d0698999c7b4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import ctypes
import datetime
import time
import multiprocessing
import itertools
libc = ctypes.CDLL('libc.so.6')
class struct_tm(ctypes.Structure):
_fields_ = [
('tm_sec', ctypes.c_int),
('tm_min', ctypes.c_int),
('tm_hour', ctypes.c_int),
('tm_mday', ctypes.c_int),
('tm_mon', ctypes.c_int),
('tm_year', ctypes.c_int),
('tm_wday', ctypes.c_int),
('tm_yday', ctypes.c_int),
('tm_isdst', ctypes.c_int),
('tm_gmtoff', ctypes.c_long),
('tm_zone', ctypes.c_int)]
strptime_proto = ctypes.CFUNCTYPE(ctypes.c_char_p, ctypes.c_char_p,
ctypes.c_char_p,
ctypes.POINTER(struct_tm))
strptime = strptime_proto(('strptime', libc), ((1, 's'),
(1, 'format'),
(2, 'tm')))
def strptime_errcheck(result, func, args):
if result is None:
raise ValueError("strptime error.")
return datetime.datetime(args[2].tm_year + 1900,
args[2].tm_mon,
args[2].tm_mday,
args[2].tm_hour,
args[2].tm_min,
args[2].tm_sec)
strptime.errcheck = strptime_errcheck
res = strptime(b"2001-11-12 18:31:01",
b"%Y-%m-%d %H:%M:%S")
print('libc.strptime: parse "2001-11-12 18:31:01" against format "%Y-%m-%d %H:%M:%S":', res)
ts1 = time.time()
for _ in range(1000000):
datetime.datetime.strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S")
print("datetime.strptime single thread:", time.time() - ts1)
ts2 = time.time()
for _ in range(1000000):
res = strptime("2001-11-12 18:31:01".encode('ascii'),
"%Y-%m-%d %H:%M:%S".encode('ascii'))
print("libc.strptime single thread:", time.time() - ts2)
def T1(args):
return datetime.datetime.strptime(*args)
def T2(args):
return strptime(*args)
ts3 = time.time()
with multiprocessing.Pool(2) as p:
it = p.imap(T1,
itertools.repeat(("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S"), 1000000),
1000)
for _ in it:
pass
print("datetime.strptime MP:", time.time() - ts3)
ts4 = time.time()
with multiprocessing.Pool(2) as p:
it = p.imap(T2,
itertools.repeat((b"2001-11-12 18:31:01", b"%Y-%m-%d %H:%M:%S"), 1000000),
1000)
for _ in it:
pass
print("libc.strptime MP:", time.time() - ts4)
@Snawoot
Copy link
Author

Snawoot commented May 22, 2019

Output (on Intel Pentium G4400, Fedora 30, Python 3.7):

[user@dt1 ~]$ ./ptime.py 
libc.strptime: parse "2001-11-12 18:31:01" against format "%Y-%m-%d %H:%M:%S": 2001-10-12 18:31:01
datetime.strptime single thread: 8.794349908828735
libc.strptime single thread: 1.8633613586425781
datetime.strptime MP: 6.025797128677368
libc.strptime MP: 1.9151949882507324

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment