4 ms·
you can do that in a lot lesser line of code in python and much better performance.
by v3ss0n 3y ago
you can do that in a lot lesser line of code in python and much better performance.
- sgarland 3y agoI suspect the performance part is only true if you're familiar with Python's stdlib performance quirks, like how `datetime.fromisoformat()` is orders of magnitude faster than `datetime.strptime()` (which would likely be the function you'd reach for if not familiar), or at the very least, that directly casting slices of the string into ints is in between the two. This example is parsing a file of 10,000,000 ISO8601 datetimes, then counting those between `HH:00:SS – HH:02:SS` inclusive. The count method is the same, and likely benefitting some OS caching, but the parse times remained constant even with repeated runs. $ python3 times_python.py strptime_listcomp parse time: 45.96 seconds count time: 0.54 seconds count: 498621 $ python3 times_python.py slices parse time: 9.96 seconds count time: 0.40 seconds count: 498621 $ python3 times_python.py isofmt parse time: 0.80 seconds count time: 0.38 seconds count: 498621