Telephone number generator using the Cartesian product methods in Python and Rust.
mbrav 5fb094e680 Clippy fixes | 1 rok temu | |
---|---|---|
.github | 1 rok temu | |
.vscode | 1 rok temu | |
rust | 1 rok temu | |
src | 1 rok temu | |
.gitignore | 1 rok temu | |
LICENSE | 1 rok temu | |
README.md | 1 rok temu | |
poetry.lock | 1 rok temu | |
pyproject.toml | 1 rok temu |
Telephone number generator using the Cartesian product methods in Python and Rust.
The Python implementation uses a Cartesian product generator from itertools.product
which takes advantage of C optimizations.
To run program:
python3 src
Additional args are possible:
python3 src --country_code 8 --prefix 999 --chunk_size 1000 --file result.txt
The --chunk_size
flag changes the number of elements that are stored in memory before being dumped to a file. Otherwise, to store a combination of 10 digit phone numbers will require more than 10GB of RAM.
In Python 3.10, the program will take roughly:
8 999 xxx xxxx
.8 99x xxx xxxx
.8 9xx xxx xxxx
.
Profiling
This will run all phone number permutations with chunk_size set to 10000 and prefix set to "999".
pip3 install snakeviz
python3 -m cProfile -o phone_gen.prof src/__main__.py && snakeviz phone_gen.prof
As of 0.1.0, the Rust implementation of the same Python program has many areas where it can be optimized, potentially making it 100X faster. Additional improvements can be made by implementing an iterator, rather than a naive list comprehension, and optimizing the algorithm that calculates the cartesian_product. Despite these numerous fallbacks, the program is 2X faster compared to its Python counterpart. The timing of the program is also much more deterministic than its Python counterpart.
The Rust implementation will take roughly:
8 999 xxx xxxx
.8 99x xxx xxxx
.8 9xx xxx xxxx
.To run the program first install Rust's toolchain with rustup
, then run:
cd rust
Run the program with build optimizations where arguments after --
are country code, prefix and output file (arbitrary path supported):
cargo run -- --country-code 8 --prefix 999 --file result.txt
The output will be the following:
telnum_gen v0.1.0
RUNNING PROGRAM
Country Code: 8
Prefix: 999
Output to: result.txt
Elements: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Combinations: 7
Elapsed: 4.894s