Skip to content
shrinish123Public

About

A faster dateparser: same API and results, common date formats read in microseconds

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

flashdate

A faster dateparser. Same API, same results, and the date formats that most data is written in are read in a few microseconds instead of a few hundred.

import flashdate as dateparser

dateparser.parse("2024-03-05T13:45:00Z")
dateparser.parse("March 5, 2024 1:45 PM")
dateparser.parse("le 5 mars 2024")
dateparser.parse("3 days ago")

flashdate is an independent, modified copy of dateparser 1.5.1. It is not made, endorsed or supported by the maintainers of dateparser: please report problems with it here, not to them.

Install

pip install flashdate

It installs next to dateparser, under its own import name, so you can run both in one program and compare them. Python 3.10 or later.

How much faster

Microseconds per date string, one thread, every string different, with CPython 3.12 on a standard GitHub Actions runner (Ubuntu, Intel Xeon 6975P-C). The table is copied from a run of the Benchmark workflow of this repository, which anyone can open, or start again in a fork.

Format, with an example dateparser flashdate Speedup
2024-03-05 672 4.8 141x
2024-03-05 13:45:00 896 5.4 166x
2024-03-05T13:45:00Z 1,388 5.4 259x
2024-03-05T13:45:00+05:30 1,338 5.5 244x
03/05/2024 679 5.3 129x
25/03/2024 662 5.7 115x
3/5/2024 13:45 830 5.7 146x
March 5, 2024 691 5.3 131x
Tue, 05 Mar 2024 13:45:00 GMT 1,460 6.1 241x
le 5 mars 2024 935 6.0 156x
5 de marzo de 2024 877 6.2 141x
5 Март 2024 841 6.3 133x
3 days ago 391 41.7 9x
Text that is not a date 7,443 432 17x

The times depend on the machine, the speedup much less. That run measured three runners with three different processors: the first row took flashdate 3.9, 4.8 and 7.9 microseconds, and was 125, 141 and 133 times faster than dateparser. Over the three, the formats above 3 days ago were 103 to 259 times faster, 3 days ago 8 to 10 times, and text that is not a date 16 to 19 times.

On the 300,000 article publication dates that dateparser keeps for its own evaluation, these fast paths read 80% of the dates. Parsing the 248,950 distinct strings of that set takes 63 seconds instead of 491, on one core of an Apple M-series laptop.

Date strings in other shapes go through the same steps as in dateparser, which are also faster: 2 to 5 times for most of them. Among those are two-digit years, hyphenated dates like 5-Mar-2024, dates with a time or a weekday in languages other than English, and free-form phrases.

First calls

What a library does once, such as loading a language and compiling its patterns, is paid by the first calls of a process. Milliseconds of the first call or calls in a new process, after the import, median of 15 processes, with CPython 3.12 on a standard GitHub Actions runner (Ubuntu), from a run of the Benchmark workflow:

First in a new process dateparser flashdate
Only the import 32.4 35.5
2024-03-05 71.4 2.1
March 5, 2024 73.9 2.2
le 5 mars 2024 112.0 2.3
3 days ago 74.6 47.7
Miércoles, 15 de Abril de 2020 98.8 83.8
20 septembrie 2020 16:20 286.5 184.8
Text that is not a date 1,014.1 312.8
Strings of web pages, one after the other 1,151.1 551.7
Tuesday 5 March 24 at 10:15 73.5 95.9
search_dates on a sentence, languages=['en'] 117.0 107.6
search_dates on a sentence 1,420.5 1,896.3

Two first calls are slower than in dateparser: an English string outside the fast paths, and search_dates without languages, which makes every language build its word-splitting pattern. The calls after them are faster than in dateparser. Import takes about 3 ms longer.

How much memory

Megabytes, with CPython 3.12 on a standard GitHub Actions runner (Ubuntu), from the same run. Each case is parsed in a new process. Peak is the most memory the process held at any moment; Python alone peaks at 16.5. Kept is the memory of the Python objects alive after the last string, which is what the library holds on to.

Parsed in a new process Peak, dateparser Peak, flashdate Kept, dateparser Kept, flashdate
Only the import 28.2 27.3 5.5 5.8
One ISO date 32.7 29.8 8.0 6.1
10,000 ISO and numeric dates 33.5 34.6 8.1 8.7
10,000 dates with English month names 33.5 34.6 8.1 8.7
10,000 dates in French, Spanish and Russian 35.6 32.6 9.8 6.1
10,000 relative dates 33.3 32.6 8.0 7.2
1,000 texts that are not dates 66.8 44.5 41.4 21.5
10,000 strings of every kind above 68.8 47.0 42.4 23.0
50,000 strings of every kind above 71.9 50.1 42.4 23.0
10,000 dates in the 205 languages 81.5 93.1 54.6 64.0

Neither library grows with the number of strings. A string that no language reads makes both libraries load every language: dateparser then keeps about 42 MB and flashdate about 23 MB, because flashdate compiles the patterns of a language only when that language reads a string.

The last case is where flashdate costs memory. When dates in every language go through one process, every language compiles its patterns, and flashdate keeps about 9 MB more than dateparser. It is a fixed amount, which does not grow with the number of strings. These are the structures that make the strings outside the fast paths 2 to 5 times faster, measured on a laptop:

What flashdate holds that dateparser does not About
Results of analysing each pattern (which letters and words it needs) 3.2 MB
Memos of recently seen words, formats and time zones 2.5 MB
Word-splitting patterns in their faster form, and smaller tables 2 MB
The words of each language, used to skip a language by its words 1.8 MB

The fast paths themselves cost almost no memory.

To measure speed, first calls and memory on your machine, see bench/.

How it is the same

The fast paths only take a date string that they are sure to read as dateparser does, with the settings in use. Everything else is read by the code of dateparser.

Each release is compared with the unmodified dateparser it is built from: 112 million date strings in the formats of the fast paths, 836,052 results of a corpus of real and malformed strings under 31 combinations of settings, and the 248,950 article dates above. There is no difference in the date, the time zone, the period, the detected language or the error raised. The test suite of dateparser passes on CPython 3.10 to 3.14.

verification/ has the method, a script that runs the comparison on your machine in a few minutes, and the limits of these checks.

What it does not change

The supported languages, the settings, search_dates, the calendars and the command line tool are those of dateparser. The documentation of dateparser applies, with flashdate as the name to import.

These are not given the fast paths and run as in dateparser, with its smaller speed-ups: a subclass of DateDataParser, and a parser with RELATIVE_BASE, date_formats, locales or region.

Relation to dateparser

flashdate 1.5.1.2 is built from dateparser at commit ce8c4390: version 1.5.1 and the fixes merged after it. The first commit of this repository is the library and tests of that commit with the package renamed, so the history after it is exactly what flashdate changes.

New formats, languages and fixes to what a date string means belong in dateparser. flashdate takes them over when it is rebuilt from a newer version, and its version number says which: 1.5.1.N is the Nth flashdate release on dateparser 1.5.1.

License

BSD 3-Clause, the license of dateparser. See LICENSE and NOTICE.

About

A faster dateparser: same API and results, common date formats read in microseconds

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages