Settings

Theme

Shock Result? Rust faster than Python in a single test of file parsing

coralbark.net

1 points by jonquark 5 years ago · 2 comments

Reader

thiasdflkj 5 years ago

why the `fp.readline()` and `while line:` business?

I don't have a big file handy to benchmark but that pattern moves the file reading into python bytecode.

The canonical way to read a file line by line while counting the lines is to loop over the file (it's automatically a line iterator) and use enumerate.

here is the disassembed bytecode of the respective versions

https://pastebin.com/Rd4g8cE2

that being said, Rust will still outrun Python overall.

  • jonquarkOP 5 years ago

    The file reading isn't the slow part - removing the regular expression and file is read in fractions of a second rather than minutes (as the last python example shows).

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection