Here is an example of Rust code that generates a CSV file with 1 billion rows:
rust
use std::fs::File;
use std::io::{BufWriter, Write};
fn main() {
let file = File::create("rust_output.csv").expect("Could not create file");
let mut writer = BufWriter::new(file);
for i in 0..1_000_000_000 {
writeln!(writer, "{}, {}", i, i * 2).expect("Could not write to file");
}
}
This code creates a file named "rust_output.csv" and writes 1 billion rows to it, with each row containing two columns: the row number and the row number multiplied by 2.
And here is an equivalent Python script:
python
import csv
with open('python_output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for i in range(1_000_000_000):
writer.writerow([i, i * 2])
This Python script creates a file named "python_output.csv" and writes 1 billion rows to it, with each row containing two columns: the row number and the row number multiplied by 2.
Note that running these scripts will generate very large files (approximately 20 GB each) and may take a significant amount of time to complete.
You can use the following command to time the execution of the Rust code:
bash
time cargo run
And the following command to time the execution of the Python script:
bash
time python python_script.py
This will give you an idea of how long each script takes to generate the CSV file.
Please note that Rust is a compiled language and Python is an interpreted language, so Rust should be significantly faster than Python for this task. Also, the performance difference will be more noticeable for larger files.
Memory usage
To measure the memory usage of the scripts, you can use the following commands:
For Rust:
bash
cargo run & pmap -d <pid>
Replace <pid> with the process ID of the Rust process.
For Python:
bash
python python_script.py & pmap -d <pid>
Replace <pid> with the process ID of the Python process.
This will give you an idea of how much memory each script is using.
Comparing the files
To compare the files generated by the two scripts, you can use the following command:
bash
diff rust_output.csv python_output.csv
This will show you any differences between the two files. If the files are identical, the command will not output anything.
System specs
The performance difference between the two scripts will depend on your system specs, including the CPU, memory, and disk speed.
Here are some system specs that may affect the performance:
CPU: A faster CPU will result in faster execution times for both scripts.
Memory: More memory will allow the scripts to handle larger files without running out of memory.
Disk speed: A faster disk will result in faster write times for both scripts.
Keep in mind that the performance difference between the two scripts will be more noticeable for larger files and slower systems.
“Rust code to generate a csv with 1 billion rows, also python script to be able to compare.” Conversation with llama-3.1-405b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850tajsaayghw1bywjg0yfg